tags:

views:

1085

answers:

2

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

+1  A: 

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

Peter Perháč
+20  A: 

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 class <typename T> 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.

Not Sure
Well worded answer.
BobbyShaftoe
+1. Sometimes it's useful for whatever reason to distinguish the "origin" of a class, in which case you can reasonably use the term "template class" -- but for the reasons you gave, it's a good idea to define carefully what you mean by this.
j_random_hacker
@j_random_hacker - see litb's answer below, but in short, the preferred term for that is "class template specialization"
Not Sure
@Not Sure: Well actually I find "class template specialization" slightly ambiguous, since it can also refer to the process of defining an explicit or partial specialisation for a template -- something that's not needed to produce an actual "class". :) IMHO "class template instantiation" is the clearest term.
j_random_hacker
@j_random_hacker: There is a proper term for it - "explicit class template specialization" and "partial explicit class template specialization.". Cumbersome, but not ambiguous :)
Not Sure