Fuzzy wording of declaration in template declaration

14/1 [temp] provides:

The declaration in the template declaration must

(1.1) - declare or define a function, class or variable, or

(1.2) - define a member function, member class, member enumeration or static data element of a class template or class nested in a class template, or

(1.3) - define a member template of a class or class template, or

(1.4) - be a declaration of aliases.

The second bullet is unclear because it looks like we cannot declare and define a member of the class template. But actually we can do this:

 template <class U> struct A { template<class T> void foo(); }; int main(){} 

CLANG

g ++

And this compiles both clang and gcc. Can oh explain what that means?

+6
source share
1 answer
 template <class U> struct A { template<class T> void foo(); }; 

As you can see, the declaration is void foo(); , which is a valid function declaration. Thus, the first marker point is applied:

The declaration in the template declaration must

  • declare or define a function, class, or variable, or
  • define a member function, member class, element enumeration, or static data element of a class template or class nested in a class template or

Member functions are mentioned in the second pool only to emphasize that

 template<class T> void A<T>::foo() {} 

; The declaration in this template declaration ( void A<T>::foo() {} ) is the definition of a member function. "Or" is not exclusive, since the above declaration is still a function definition.

Now it becomes clear that this can only concern definitions, since

 template<class T> void A<T>::foo(); 

not valid anyway.

+3
source

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


All Articles