开发者

Class templates and template class

开发者 https://www.devze.com 2023-01-02 05:19 出处:网络
Is there开发者_如何学运维 a difference between a class template and template class. If so what is it?When both terms are used there is a very subtle difference. It is more linguistic than semantic, it

Is there开发者_如何学运维 a difference between a class template and template class. If so what is it?


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:

  • What is the difference between a template class and a class template?
  • Is there a difference between a function template and a template function, or between a class template and a template class? — link suggested by Josh Haberman


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.)


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;


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


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


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


 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消