472,127 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Why is template function not allowed to have defaut arguments?

Why is template function not allowed to have defaut arguments?

We know that class template is allowed to have default arguments in C++
standard, why is template function not? I can't think out the reason
for doing so. Who can tell me why?

Thanks in advance.

Jul 11 '06 #1
4 1815
Lighter wrote:
Why is template function not allowed to have defaut arguments?

We know that class template is allowed to have default arguments in
C++ standard, why is template function not? I can't think out the
reason for doing so. Who can tell me why?
Somebody in comp.std.c++ can -- they usually provide the rationales
behind the Standard.

If I were to speculate, I'd say that it's not really necessary. Can
you think of a case that cannot be solved using any other means but
the default template arguments? Just don't make it a template.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '06 #2
Victor Bazarov wrote:
Lighter wrote:
Why is template function not allowed to have defaut arguments?

We know that class template is allowed to have default arguments in
C++ standard, why is template function not? I can't think out the
reason for doing so. Who can tell me why?
In C++ Templates: The Complete Guide, the authors give a rationale for
this(chapter 13). Essentially when function templates were added to the
language even explicit function template arguments were not valid and
function template arguments had to be deduced from the call expression.
Hence default template arguments were not of any use since they would
always be overridden. Maybe in the future the standard might allow it
also.

Thanks and regards
SJ

Jul 11 '06 #3
Lighter posted:
Why is template function not allowed to have defaut arguments?

The following compiles just fine for me:
#include <string>

template<class T>
void Func( T const &obj = T() ) {}

int main()
{
Func<double>();
Func<std::string>();
}


--

Frederick Gotham
Jul 11 '06 #4
In message <2w*******************@news.indigo.ie>, Frederick Gotham
<fg*******@SPAM.comwrites
>Lighter posted:
>Why is template function not allowed to have defaut arguments?


The following compiles just fine for me:
He means default _template_ arguments

template <class T, class U=int// not allowed
U func(T t)
{
U result;
// do stuff...
return result;
}
>
#include <string>

template<class T>
void Func( T const &obj = T() ) {}

int main()
{
Func<double>();
Func<std::string>();
}

--
Richard Herring
Jul 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by CoolPint | last post: by
14 posts views Thread by Bart Samwel | last post: by
5 posts views Thread by Niklas Norrthon | last post: by
16 posts views Thread by xman | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.