Reputation: 60381
The following compile with no problem in g++ :
template<typename ReturnType = double, typename OtherType> ReturnType func(const OtherType& var)
{
ReturnType result = 0;
/* SOMETHING */
return result;
}
Is it ok for all standard-compliant compilers to have a not-defaulted template parameter (OtherType
here) after a defaulted template parameter (ReturnType
here) ?
Upvotes: 7
Views: 173
Reputation: 185681
It's complicated. From the C++11 spec:
If a template-parameter of a class template has a default template-argument, each subsequent template- parameter shall either have a default template-argument supplied or be a template parameter pack. If a template-parameter of a primary class template is a template parameter pack, it shall be the last template- parameter . [ Note: These are not requirements for function templates or class template partial specializations because template arguments can be deduced (14.8.2).
So what you're trying to do is not allowed for classes, unless it's a partial specialization. But for functions it's ok.
So as long as you're only doing this trick with functions as you show in your example, it's ok. You just can't generalize that to class templates.
Upvotes: 11