What is the difference between a template class and a class template?
What is the difference between a template class and a class template?
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.
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.
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.
A template class is related to the Template Method design pattern, while class template is just a "fill-in-the-blanks" class template.
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.