Why is template function not allowed to have defaut arguments?

L

Lighter

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.
 
V

Victor Bazarov

Lighter said:
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
 
S

sonison.james

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
 
F

Frederick Gotham

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>();
}
 
R

Richard Herring

Frederick Gotham said:
Lighter posted:



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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top