Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

templates

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.

like image 923
mgregor Avatar asked Dec 20 '11 10:12

mgregor


People also ask

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.

What is template name the type of template?

There are three kinds of templates: function templates, class templates and, since C++14, variable templates. Since C++11, templates may be either variadic or non-variadic; in earlier versions of C++ they are always non-variadic.

What can the template parameter in C++ template definition be?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What are templates How are templates used to define classes and functions?

Class Templates like function templates, class templates are useful when a class defines something that is independent of the data type. Can be useful for classes like LinkedList, BinaryTree, Stack, Queue, Array, etc. Following is a simple example of a template Array class.


1 Answers

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.
like image 103
kennytm Avatar answered Oct 23 '22 12:10

kennytm