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) ?
Template parameters may have default arguments. The set of default template arguments accumulates over all declarations of a given template.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With