简体   繁体   中英

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 does 1 and 2 compile whereas 3 does not?

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

(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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM