7

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) ?

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.