Default type parameter error in template code

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 are 1 and 2 compiled, but 3 not?

+6
c ++ templates
source share
3 answers

For the same reason why:

 void f(int = 0, int); 

not executed.

Cannot use default parameter of third version:

 template<class T = int, class U> class B { ... }; B<, short> var; // ??? no such syntax 
+8
source share

(3) poorly formed because

C ++ 03 [ Section 14.1/11 ] says

If the template parameter has a default template argument, all subsequent template parameters must have the default template argument specified .

+6
source share

If you put this in some context, the third way may be legal, provided that the second default option was specified 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; } 
+6
source share

All Articles