Reputation: 3325
What is the difference between a template class and a class template?
Upvotes: 105
Views: 57055
Reputation: 5270
Take a look at N0209: Proposed Revisions to the Template Specification:
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; }
Upvotes: 1
Reputation: 543
I guess The Standard C++ Foundation is the most authoritative source on the subject:
...template classes (instantiations of class templates)...
https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl
Upvotes: 0
Reputation: 29
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.
Upvotes: 2
Reputation: 3316
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.
Upvotes: 30
Reputation: 1
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)
Upvotes: -1
Reputation: 507005
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; };
Upvotes: 18
Reputation: 5963
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.
Upvotes: 151
Reputation: 20782
A template class is related to the Template Method design pattern, while class template is just a "fill-in-the-blanks" class template.
Upvotes: 5