mgregor
mgregor

Reputation: 61

Using template-name in place of template-id inside class template definition

Is the following C++ code correct? And if so, could anybody point me to a paragraph in the standard that mentions this? It seems that one can use template-name instead of template-id in a scope enclosed with template and the compiler automatically adds the template argument list.

template<class Type>
class Mana {
public:
  Mana(const Mana& m) {/*...*/}
  Mana() {/*...*/}
};

as opposed to:

template<class Type>
class Mana {
public:
  Mana(const Mana<Type>& m) {/*...*/}
  Mana() {/*...*/}
};

The code compiles with g++ as well as in MS visual studio.

Upvotes: 6

Views: 136

Answers (1)

kennytm
kennytm

Reputation: 523314

Yes the code is correct. (The quote: §14.6.1[temp.local]/2

Within the scope of a class template specialization or partial specialization, when the injected-class-name is used as a type-name, it is equivalent to the template-name followed by the template-arguments of the class template specialization or partial specialization enclosed in <>. [ Example:

template<template<class> class T> class A { };
template<class T> class Y;
template<> class Y<int> {
   Y* p;        // meaning Y<int>
   Y<char>* q;  // meaning Y<char>
   A<Y>* a;     // meaning A<::Y>
   class B {
       template<class> friend class Y;   // meaning ::Y
   };
};

end example ]

)

In fact this is used all over the place in the standard as well, e.g.

// §20.4.2.1[tuple.cnstr]/10-13
tuple(const tuple& u) = default;
tuple(tuple&& u) = default;

// §21.4.6.1[string::op+=]/1-2
basic_string& operator+=(const basic_string& str);

// etc.

Upvotes: 7

Related Questions