tags:

views:

333

answers:

7

Is there a difference between a class template and template class. If so what is it?

+1  A: 

No, the two terms refer to the same thing. In both cases, it is a class defined as:

template<typename T> class Foo {};

This is in contrast to a regular class which is just declared as:

class Foo {};

"Template Class" makes sense if you think of it as a special kind of class. "Class Template" makes sense if you think of it as not actually a class, but a template for a class. The latter is a bit more accurate, since no actual class exists until the template is instantiated, eg:

Foo<char> x;
Josh Haberman
The C++ FAQ Lite seems to suggest there is a difference: "A note to the experts: I have obviously made several simplifications above. This was intentional so please don't complain too loudly. If you know the difference between a .cpp file and a compilation unit, **the difference between a class template and a template class**, ..." Any idea what he's talking about?
They're probably calling a "template class" an instantiation of a class template. This is a somewhat subtle distinction, and I think that people more often mean them interchangeably. See: http://womble.decadentplace.org.uk/c++/template-faq.html#phrase-order
Josh Haberman
*"refer to the same thing"* - not exactly, they are just often used synonymously.
Georg Fritzsche
If Stroustrup uses them interchangeably (which he does in both his book and his FAQ), I think it's safe to say that they mean the same thing.
Josh Haberman
@user168 the C++ FAQ simply is wrong at that point. There is no difference regarding C++. The term "template class" simply does not exist. I mailed its author months ago, and he agreed with me on it, but he hasn't fixed it since then (apparently, he was seeking to rewrite the templates chapters anyway, (he was asking me to supply some more template questions). But that's speculation on my part :)).
Johannes Schaub - litb
+1  A: 

A class template is a template for creating a class. A template class is a class created as an instantiation of a template.

Jerry Coffin
+1  A: 

I think of a class template as a kind of template, and a template class as a class instantiated from a class template.

Big Al
+8  A: 

When both terms are used there is a very subtle difference. It is more linguistic than semantic, it depends on which word you are modifying.

In short, a class template is a particular kind of template. Templates can define either classes or functions. A class template is a template that defines a class. See the difference:

template <typename T> class SomeClass {...};      // this is a class template
template <typename T> int some_function(T&) {...} // this is a function template

A template class is a particular kind of class. There are many kinds of classes, and in particular, template classes are those defined using a class template. Contrast:

SomeClass sc;         // SomeClass is an ordinary (non-template) class
SomeClass<int> sc;    // SomeClass<int> is a template class

From Stroustrup's C++ Glossary:

template class - class parameterized by types, values, or templates. The template arguments necessary to identify the class to be generated for the class template must be provided where a template class is used. For example "vector<int> v;" generates a vector of ints from the vector template. See also template. TC++PL 13.2, D&E 15.3.

Both expressions are used in Stroustrup's book The C++ Programming Language, and the ISO/IEC C++ standard until 1998.

Note: As discussed in the comments below, it seems that C++03 doesn't use the term "template class" anymore (although I don't have a copy of it), presumably to reduce confusion. As I said before, they are fundamentally the same thing, it is just a linguistic difference: in the templates context you refer to a particular kind of template or in the classes context you refer to a particular kind of class. If you just stick to "class template", you won't lose anything.

More food for thought:

Juliano
I have my Stroustrop book open (The C++ Programming Language), and he frequently uses "template class" to mean "class template." For example, from 13.2: "The standard library provides the template class basic_string that is similar to the templated String." But basic_string is a class template. I agree that you can draw this distinction if you want, but people most often use the two terms interchangeably.
Josh Haberman
Also, I don't think the Stroustrup glossary entry supports your point. If you look up "class template" there, it says "see 'template class'", so the glossary is considering them the same thing.
Josh Haberman
Also, I don't believe the terms "template class" or "template function" appear in newer versions of the standard, see: http://womble.decadentplace.org.uk/c++/template-faq.html#phrase-order
Josh Haberman
@Josh: Stroustrup is correct. basic_string itself is a *template class* since it can be parametrized on types. It is defined through the *class template* `template<class Ch, class Tr, class A> class std::basic_string...` which resides in the <string> header.
Juliano
I have both TC++PL 3rd ed. and the ISO/IEC 14882:1998 standard here, both in electronic and paper forms, and I can confirm that all variants appear in both. Chapter 14.5.1 of the standard is "Class templates", chapters 18.2.1.1, 20.3.6.1-3, 20.4.5, 21.3, etc... are about many "template classes".
Juliano
@Josh: Stroustrups FAQ entry mentions both terms, thus the *see also* (*"generated for the class template"*). The womble link only refers to a cleanup of the usage of the terms, C++03 contains both.
Georg Fritzsche
@Juliano: yes, both terms are used, but the distinction you are trying to draw is not correct. If you are going to draw a distinction (which I don't believe Stroustrup or the C++ standard do), you would say that basic_string is a class template, but basic_string<char> is a template class (this is, for example, what your Comeau link above is saying). It is nonsensical to say (as you do) that basic_string is a template class, but that it is declared with a class template.
Josh Haberman
In the Standard though, the term *template class* is not used anymore. It was used in the C++98 standard sometimes as being synonymous with *class templates* and sometimes as meaning a different thing (the difference was made in pre-Standard C++ and is explained in the ARM). In the C++03 revision, this term was gotten rid of, and the term *class template specialization* is used instead of *template class*.
Johannes Schaub - litb
In fact, using the meaning of ARM, the Standard library was defective in many places to use the term "template class". `basic_string` simply is a *class template* under the ARM, while `basic_string<char>` would for instance be a *template class* under the ARM. More specifically, the ARM defined "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 latter refers to explicit specializations).
Johannes Schaub - litb
So @Josh is correct, this answer draws a wrong distinction.
Johannes Schaub - litb
*If you start arguing about whether you should have a space in between the int and the star in a pointer-to-int, you get a REALLY furious discussion. People actually LOVE discussing things that don't matter, because it's so easy to have an opinion about those things.* — Bjarne Stroustrup.
Juliano
@Johannes: Since the beginning I'm trying to say that the difference is *linguistic* rather than semantic. I think that it is pretty obvious that both are the *same* thing, but seen from different contexts. Saying that the answer draws a wrong distinction is wrong on your part. It looks like you are trying to be very vocal on a moot point.
Juliano
@Juliano i just objected to you saying that the C++ Standard has this distinction, and i was agreeing to @Josh that, even if your distinction was purely linguistic, the distinction you draw makes no sense to me. As you wrote it, to me it was all but obvious that you think that they are the same. I'm also not arguing that they are the same - i'm saying that it depends on the person you ask. The Standard doesn't define and didn't even define it prior to '03. It used it in arbitrary combination, without any linguistic intentions visible to me.
Johannes Schaub - litb
+1  A: 

Nowadays the two are used interchangeably because of the influence of C++. In the old days, pre-C++ templates, however, a template class was part of object-oriented design terminology. The idea behind a template class was that it formed the framework of execution with clearly defined, overridden "hotspots" (virtual methods in implementation) that subclasses would provide the functionality in. The effect was something like the following pseudocode:

class templateClass:
  public:
    virtual hotSpotA: nil
    virtual hotSpotB: nil
    method templateExecute:
      do some stuff
      hotSpotA
      do some other stuff
      hotSpotB
      do even more stuff

You would then use this template class by inheriting from it and implementing the hotSpots:

class templateClient from templateClass:
  public:
    override hotSpotA:
      client-specific stuff A
    override hotSpotB:
      client-specific stuff B

client = new templateClient
client.templateExecute

A lot of design meta-patterns relied upon this kind of structure (and, indeed, if you look at most OO design patterns you'll see this kind of structure in various forms used all the time).

Nowadays you'll not see this terminology used this way very much any more because of the pernicious influence of C++. I'm not sure what the current term for this meta-pattern structure is because I've pretty much ditched OOP entirely except when forced to use it.

There was a book that explained this (and other meta-patterns) in great detail should you wish to read it: Design Patterns for Object-Oriented Software Development

JUST MY correct OPINION
People still talk about the Template Method Pattern, but (in my experience) they capitalise Template, so the distinction between Template Method and C++ templates is clear. Although I note that the wikipedia article is hopelessly inconsistent in capitalising the pattern name. Personally I don't remember mentions of "Template classes" as the classes which contain Template Methods, but it wouldn't confuse me. So is it really "pernicious influence", or just not that much need for a special term for that class? I know of no other name for the pattern (except maybe that it's a kind of IoC).
Steve Jessop
+3  A: 

This question is also addressed specifically in the definitive book on C++ Templates ("C++ Templates: The Definitive Guide", by Vandvoorde and Josuttis), Section 7.1, p.87 (at least in my edition):

7.1 "Class Template" or "Template Class"?

[snip]

There is some confusion about how a class that is a template is called:

The term class template states that the class is a template. That is, it is a parameterized description of a family of classes.

The term template class on the other hand has been used

  • as a synonym for class template.

  • to refer to classes generated from templates.

  • to refer to classes with a name that is a template-id.

The difference between the second and third meaning is somewhat subtle and unimportant for the remainder of the text.

Because of this imprecision, we avoid the term template class in this book.

Similarly, we use function template and member function template, but avoid template function and template member function.

(Anyone, I mean anyone, working with C++ templates should have a copy of this book, by the way.)

Dan
that is a good reference
Chubsdad
+1  A: 
 What is the difference between a template class and a class template?
 What is the difference between a template function and a function template?

The question is: What is being modified? Generally speaking, if we say something is a "green house" the object is a house, that happens to be green. With that in mind, this is clearly a class:

struct SomeClass {
   int i;
   };

whereas this is clearly a template:

 template <typename T>
 struct cTemplate {
 int member;
 };

What kind of template is it? cTemplate a class template. Along these same lines, this is a function:

void foo() {}

and this is a template:

template <typename T>
T fTemplate(T arg)
{
 return arg * arg;
}

What kind of template is it? fTemplate is a function template.

Now, SomeClass is a class name, and hence allows us to write something such as:

SomeClass sc;

Along these same lines, we know we can do something such as:

cTemplate<int> cti;

In other words, cTemplate is a class name too. So, cTemplate is a class. Think about it, it'll effectively be this when it is instantiated:

template <> struct cTemplate<int> {
  int member;
}

What kind of class it is? Well, it's probably not necessary to qualify what kind, but if it is, it would seem to follow that cTemplate is a template class. Similarly, with functions. That is, if one were to call fTemplate, for instance:

fTemplate(99.99);

then it following that a function is instantiated:

fTemplate<double>(double)

In particular, this:

template <>
double fTemplate<double>(double)
{
  return arg * arg;
}

So, as above, if pressed for what kind of function this is, it seems fair to say that it is a template function.

The above all said, it turns out that some folks use template class to mean the same thing as class template, so because of that, some (other) people prefer not to place a specific meaning on the former term.

BE Student