What is the difference between a template class and a class template?

C++

C++ Problem Overview


What is the difference between a template class and a class template?

C++ Solutions


Solution 1 - C++

This is a common point of confusion for many (including the Generic Programming page on Wikipedia, some C++ tutorials, and other answers on this page). As far as C++ is concerned, there is no such thing as a "template class," there is only a "class template." The way to read that phrase is "a template for a class," as opposed to a "function template," which is "a template for a function." Again: classes do not define templates, templates define classes (and functions). For example, this is a template, specifically a class template, but it is not a class:

template<typename T> class MyClassTemplate
{ 
    ...
};

The declaration MyClassTemplate<int> is a class, or pedantically, a class based on a template. There are no special properties of a class based on a template vs. a class not based on a template. The special properties are of the template itself.

The phrase "template class" means nothing, because the word "template" has no meaning as an adjective when applied to the noun "class" as far as C++ is concerned. It implies the existence of a class that is (or defines) a template, which is not a concept that exists in C++.

I understand the common confusion, as it is probably based on the fact that the words appear in the order "template class" in the actual language, which is a whole other story.

Solution 2 - C++

Bjarne Stroustrup, the creator of C++, says in his book The C++ Programming Language 4th edition, 23.2.1 Defining a Template:

> There are people who make semantic distinctions between the terms class template and template class. I don't; that would be too subtle: please consider those terms interchangeable. Similarly, I consider function template interchangeable with template function.

Solution 3 - C++

The difference is that the term "template class" does simply not exist in the C++ Standard. It's a term used mostly by people that think that the term "class template" is confusing (like the Qt companies Nokia and formerly Trolltech).

The Standard has no concept of it, so it's up to other peoples to make a difference. Some people use it synonymously, and others say that the term "template class" refers to an instantiated or explicitly specialized class template, which would make it equivalent to the term "class template specialization". Historyically, it had this meaning. The Annotated Reference Manual defines at page 343

> A class generated from a class template is called a template class, as is a class specifically defined with a template-class-name as its name

The non-terminal template-class-name is equivalent to the non-terminal template-id used in todays Standard and comes down template-name < arguments >.


To get you familiar with the today terms, which is more important than using dubious old terms

// (1) defines a class template
template<typename T> class A { }; 

// (2) defines a class template explicit specialization 
template<> class A<int> { };

// (3) defines a class template partial specialization
template<typename T> class A<T*> { };

// (4) explicitly instantiates A<char>. 
template class A<char>;

// (5) implicitly instantiates A<short> (because of the member declaration)
struct D { A<short> a; };
  • The ARM called the class (2), and the classes generated by (4) and (5) a template class. I'm not sure whether the ARM already knew about partial specializations. But if so (3) was not called a template class, because (3) does not define a class, but defines a template.
  • The current Standard calls class (2), and the ones generated by (4) and (5) class template specializations. And (3) is called a partial specialization, as opposed to an explicit specialization. It also sometimes calls (3) a specialization (3.2/5 - however with clarifying cross-links), although i find this isn't entirely clear to me, since it defines a "specialization" as being a "class, function or class member", which (3) doesn't satisfy.

Solution 4 - C++

A template class is related to the Template Method design pattern, while class template is just a "fill-in-the-blanks" class template.

Solution 5 - C++

A class template is a template that is used to generate classes whereas a template class is a class that is produced by a template.

Solution 6 - C++

Template class: A class that has generic definition or a class with parameters which is not instantiated until the information is provided by the client. It is referred to a jargon for plain templates.simply class with prefix template and use of T . Class template: The individual construction of a class is specified by a class template which is almost similar the way how individual objects are constructed by using a class. It is referred to a object of the template class Ex- classname objectname(argument list)

Solution 7 - C++

Class template is a generic class for different types of objects. Basically it provides a specification for generating classes based on parameters. Whenever a new object is created a new class will take place in the memory for that purpose. This is called instantiating a class template and each instantiated version of the class is called template class.

Solution 8 - C++

Take a look at this paper(from wg21 and was published in 1992):

> # Consistent Terminology

> Much of the argument and disagreement to date, has been with respect to the terminology used in the chapter describing templates. Most frequent has been the varying application of 'function-template' and 'template-function' to express different ideas and intents. Since no consistent naming is applied, the result is confusion and argument.

> For the purposes of this document, as a proposal for formal adoption by the committee in discussions about templates, and for the purposes of clarifying the documentation; I propose we adopt the formalisation, that a trailing '-template' describes a set of types or functions described by a template. And that a leading 'template-', is used to describe the template definition of part of a '-template', such as a 'template-member-function'. Thus :-

> * 'function-template': A set of functions described by a template, parametric on some type information provided as argument to that template. For example :-

template<class T> int nullcheck( T* pT )
{ return ( pT != 0 ); }

> * 'class-template': A set of classes described by a template, parametric on some type information provided as argument to that template. For Example :-

template<class T> class S {
int i;
public:
int sep_member();
int imm_member()
{ return 2; }
}

> * 'template-function': This term is no longer permitted.** > * 'template-class': This term is not permitted.**

> * 'member-function-template': This term is not permitted, as it describes a property not currently supported by the template definition. Using the above terminology convention, this would describe a member of a non-class-template, whose definition was itself a template. For example :-

class Normal { public:
template<class T> int foo(T*pT)
{ return ( pT == 0 ); }
};

> However, since templates are currently limited to the global scope, such a template is invalid.

> * 'template-static-member-function': > * 'template-member-function': > * 'template-static-member': > * 'template-static-data-member > 'template-member': > Alternative terms for the definition of a member appearing separate to the 'class-template' to which it belongs. For example :-

template<class T> int S<T>::sep_member()
{ return i; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncoderView Question on Stackoverflow
Solution 1 - C++Not SureView Answer on Stackoverflow
Solution 2 - C++SHHView Answer on Stackoverflow
Solution 3 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 4 - C++Peter PerháčView Answer on Stackoverflow
Solution 5 - C++DEBALINA MAHATAView Answer on Stackoverflow
Solution 6 - C++RishabhView Answer on Stackoverflow
Solution 7 - C++BlackList96View Answer on Stackoverflow
Solution 8 - C++Chen LiView Answer on Stackoverflow