tags:

views:

54

answers:

3
1)template <class T = int, class U = double> //compiles

2)template <class T, class U =double> //compiles

3)template <class T = int, class U> //fails

Why does 1 and 2 compile whereas 3 does not?

+9  A: 

(3) is ill-formed because

C++03 [Section 14.1/11] says

If a template-parameter has a default template-argument, all subsequent template-parameters shall have a default template-argument supplied.

Prasoon Saurav
Great answer +1. Where can I get a copy of the standard?
JoshD
@JoshD : You can find it [here](http://www.open-std.org/jtc1/sc22/wg21/) (ISO/IEC 14882 2003 is C++03)
Prasoon Saurav
@JoshD: AFAIK you can't get it for free. However you can look at the drafts of the next standard.
ybungalobill
Thank you both very much.
JoshD
Although, I would have liked (3) to be valid in case of function templates. We still have TAD (template argument deduction) to determine the trailing, non-defaulted template parameters.
sellibitze
@sellibitze in that case you can add a default `class = void` and it'll solve your problem.
ybungalobill
@ybungalobill: Interesting. But I don't think it'll work for argument packs.
sellibitze
+8  A: 

For the same reason why:

void f(int = 0, int);

fails.

There is no way to use 3rd version default parameter:

template<class T = int, class U> class B { ... };

B<, short> var; // ??? no such syntax
ybungalobill
+6  A: 

If you put that into some context, the third way may actually be legal, provided that the second default has been given earlier.

template <class T, class U = double>
struct X;

template <class T = int, class U> //here
struct X {};

int main()
{
    X<> x;
    X<float> y;
    X<char, char> z;
}
UncleBens
Yes, good catch, +1.
Prasoon Saurav