Typedef with templates

template<class T> struct A { typedef int Int; A::Int b; // Line 1 (fails) Int c; // Line 2 (compiles) }; int main(){ A<int> x; xc = 13; } 

Mistakes

 error: ISO C++ forbids declaration of 'Int' with no type error: extra qualification 'A<T>::' on member 'Int' error: expected ';' before 'b' 

Line 1 fails, but line 2 compiles. Why?

+4
source share
1 answer

You will need typename

 typename A::Int b; 

The typename keyword is required because the element refers to the use of the qualified name A::Int .

Int c excellent, because in this case a qualified name is not used.

14.6 / 6

In a class template definition or class template member definition, the typename keyword is not required when referring to the unqualified name of a previously declared class template element that declares a type. The typename keyword is always specified when a member refers to the use of a qualified name , even if the qualifier is simply the name of a class template.

+11
source

All Articles