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

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

+67
c ++
May 18 '09 at 20:07
source share
6 answers

This is a common point of confusion for many (including the General Programming page on Wikipedia, some C ++ lessons, and other answers on this page). As for C ++, there is no such thing as a "template class", there is only a "template template". . The way to read this phrase is a "template for a class", as opposed to a "template for a function", which is a "template for a function". Again: classes do not define patterns, patterns define classes (and functions). For example, this is a template, in particular a class template, but it is not a class:

template<typename T> class MyClassTemplate { ... }; 

The MyClassTemplate<int> declaration is a class, or pedantically, a template-based class. There are no special properties for a class based on a template and a class not based on a template. Special properties have the template itself.

The phrase "template class" does not mean anything, because the word "template" does not make sense as an adjective when applied to the noun "class" in relation to C ++. This implies the existence of a class in which there is (or defines) a template that is not a concept that exists in C ++.

I understand the general confusion, since this is probably based on the fact that the words appear in the order of the "template class" in the language itself, which is a completely different story.

+95
May 18 '09 at 20:54
source share

The difference is that the term "template class" simply does not exist in the C ++ standard. This is a term used primarily by people who think the term “class template” is confusing (for example, Qt Nokia and formerly Trolltech).

The standard has no idea about this, therefore it is important for other nations. Some people use this synonymously, while others say that the term "template class" refers to an instance or explicitly specialized template of a class, which will make it equivalent to the term "template template specialization." Historically, it has had this meaning. Annotated reference guide identifies on page 343

A class generated from a class template is called a template class, as is a class specifically defined with template-template name as its name

The non-terminal type-template-name is equivalent to the non-terminal identifier used in today's standard, and template-name < arguments > omitted.




To get familiar with today's conditions, 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; }; 
  • ARM is called the class (2) and the classes generated by the (4) and (5) template class. I'm not sure if ARM knew about partial specialization. But if so (3) was not called a template, because (3) does not define a class, but defines a template.
  • The current standard calls class (2) and those generated by (4) and (5) by class template specializations. And (3) is called partial specialization, in contrast to explicit specialization. It also sometimes calls for (3) specialization (3.2 / 5 - however with clarifying cross-references), although I think this is not entirely clear to me, because it defines "specialization" as a "class, function, or class" member ", which (3) does not satisfy.
+11
May 18 '09 at 23:44
source share

The template class is associated with the template template template template , and the template template is just a class "fill in the blanks" template.

+7
May 18, '09 at 20:10
source share

Bjarne Straustrup, creator of C ++, says in his book "C ++ Programming Language" 4th Edition, 23.2.1 Template Definition:

There are people who make semantic differences between condition class templates and a template. I do not; that would be too subtle: please consider these terms interchangeably. Similarly, I am considering a function template interchangeably with a template function.

+4
Sep 24 '16 at 22:36
source share

Template class: a class that has a common definition or a class with parameters that are not created until the information is provided by the client. It refers to jargon for simple patterns. Just a class with a prefix pattern and the use of T. Class template: the individual construction of the class is determined by the class template, which is almost the same as how individual objects are created using the class. It refers to the template class object Ex-classname objectname (argument list)

0
Nov 23 '15 at 18:19
source share

A class template is a general class for different types of objects. It basically provides a specification for generating classes based on parameters. Whenever a new object is created, a new class will have a place in memory for this purpose. This is called an instance of the class template, and each instance version of the class is called a template class.

0
Jun 17 '16 at 19:32
source share



All Articles