By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Why is template function not allowed to have defaut arguments?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.