6

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.

1
  • 1
    The code is correct. Mana is equivalent to Mana<Type>. For quote from standard, we need to wait.
    – iammilind
    Commented Dec 20, 2011 at 10:44

1 Answer 1

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.