coder coder - 2 months ago 8
C++ Question

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

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

Answer

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.

Comments