Template structure with template argument not created by default

Say I have this code

template<typename T2, typename T = int>
struct X
{
    static double f;
};

template<typename T>
double X<T>::f = 14.0;

If I try to compile clang give me the following error

The nested name specifier "X ::" for the declaration does not apply to a class, template template, or partial specialization of a class template

and for GCC:

error: non-template template definition 'double X :: f'

The question arises:

Why does the compiler want us to specialize structure X as follows:

template<typename T2>
struct X<T2,int>
{
    static double f;
};

The first declaration has a intdefault argument, why does the compiler not select this declaration?

I searched in the standard anchor [temp.spec], but that did not help.

I ask this question after answering this one on SO.

Thank you for your help!

+4
1

" , X " - , . , , , .

, template<typename T2, typename T = int> struct X , . , , , .

, , :

template<typename T2, typename T>
double X<T2, T>::f = 14.0;

(N4527, ):

[14.5.1p3]

-, -, , , . , . , . .

[14.1p9]

[...] - template-parameter-lists , . [...]


, (T2 T) , , , .

template<typename T, typename U>
double X<T, U>::f = 14.0;

X. , .


f , template<typename T> double X<T>::f = 14.0; f template<typename T2> struct X<T2,int> ( ). f template<typename, typename> struct X undefined.

[14.5.5.3p1]:

. . - . . [...]

+8

Source: https://habr.com/ru/post/1599626/


All Articles