C++ 模板类和类模板有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/879535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:46:30  来源:igfitidea点击:

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

c++

提问by coder

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

模板类和类模板有什么区别?

回答by Not Sure

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 nota class:

对于许多人来说,这是一个常见的混淆点(包括维基百科上的通用编程页面、一些 C++ 教程和此页面上的其他答案)。就C++而言,没有“模板类”这样的东西,只有“类模板”。读这个短语的方式是“类的模板”,而不是“函数模板”,后者是“函数的模板”。 再说一遍:类不定义模板,模板定义类(和函数)。例如,这是一个模板,特别是一个类模板,但不是一个类

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.

声明MyClassTemplate<int>是一个类,或者迂腐地,一个基于模板的类。 基于模板的类与不基于模板的类没有特殊的属性。特殊属性属于模板本身

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 classthat is(or defines) a template, which is not a concept that exists in C++.

短语“模板类”没有任何意义,因为就 C++ 而言,当应用于名词“类”时,“模板”一词作为形容词没有任何意义。它意味着一个的存在(或限定)一个模板,这是不存在于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.

我理解常见的混淆,因为它可能基于这样一个事实,即单词在实际语言中以“模板类”的顺序出现,这是另一回事。

回答by Johannes Schaub - litb

The difference is that the term "template class" does simply not exist in the C++ Standard. It's a term used mostly by people that think that the term "class template" is confusing (like the Qt companies Nokia and formerly Trolltech).

不同之处在于 C++ 标准中根本不存在术语“模板类”。这是一个主要用于认为术语“类模板”令人困惑的人(如 Qt 公司诺基亚和以前的 Trolltech)。

The Standard has no concept of it, so it's up to other peoples to make a difference. Some people use it synonymously, and others say that the term "template class" refers to an instantiated or explicitly specialized class template, which would make it equivalent to the term "class template specialization". Historyically, it had this meaning. The Annotated Reference Manualdefines at page 343

标准没有关于它的概念,所以要由其他人来做出改变。有些人将它用作同义词,而另一些人则说术语“模板类”是指实例化或显式专门化的类模板,这将使其等同于术语“类模板专门化”。从历史上看,它具有此含义。带注释的参考手册在第 343 页定义

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

从类模板生成的类称为模板类,因为它是专门定义的以模板类名称作为名称的类

The non-terminal template-class-nameis equivalent to the non-terminal template-idused in todays Standard and comes down template-name < arguments >.

非终端模板类名相当于今天标准中使用的非终端模板ID,并且下降了template-name < arguments >



To get you familiar with the today terms, which is 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; };
  • The ARM called the class (2), and the classes generated by (4) and (5) a template class. I'm not sure whether the ARM already knew about partial specializations. But if so (3) was not called a template class, because (3) does not define a class, but defines a template.
  • The current Standard calls class (2), and the ones generated by (4) and (5) class template specializations. And (3) is called a partial specialization, as opposed to an explicit specialization. It also sometimes calls (3) a specialization (3.2/5 - however with clarifying cross-links), although i find this isn't entirely clear to me, since it defines a "specialization" as being a "class, function or class member", which (3) doesn't satisfy.
  • ARM 将类(2) 和由(4) 和(5) 生成的类称为模板类。我不确定 ARM 是否已经知道部分专业化。但是如果这样的话(3)就没有被称为模板类,因为(3)没有定义一个类,而是定义了一个模板。
  • 当前标准调用类 (2),以及由 (4) 和 (5)类模板特化生成的。并且 (3) 被称为部分特化,而不是显式特化。有时它也调用 (3) 专业化 (3.2/5 - 但是需要澄清交叉链接),尽管我发现这对我来说并不完全清楚,因为它将“专业化”定义为“类、函数或类成员”,其中(3)不满足。

回答by SHH

Bjarne Stroustrup, the creator of C++, says in his book The C++ Programming Language 4th edition, 23.2.1 Defining a Template:

C++ 的创造者 Bjarne Stroustrup 在他的书The C++ Programming Language 4th edition23.2.1 Defining a Template 中说:

There are people who make semantic distinctions between the terms class templateand template class. I don't; that would be too subtle: please consider those terms interchangeable. Similarly, I consider function templateinterchangeable with template function.

有些人在术语class templatetemplate class之间进行语义区分。我不; 这太微妙了:请考虑这些术语可以互换。同样,我认为函数模板可以与模板函数互换。

回答by Peter Perhá?

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

模板类与模板方法设计模式相关,而类模板只是一个“填空”类模板。

回答by DEBALINA MAHATA

A class template is a template that is used to generate classes whereas a template class is a class that is produced by a template.

类模板是用于生成类的模板,而模板类是由模板生成的类。

回答by u6949852

Take a look at this paper(from wg21 and was published in 1992):

看看这篇论文(来自 wg21,发表于 1992 年)

Consistent Terminology

Much of the argument and disagreement to date, has been with respect to the terminology used in the chapter describing templates. Most frequent has been the varying application of 'function-template'and 'template-function'to express different ideas and intents. Since no consistent naming is applied, the result is confusion and argument.

For the purposes of this document, as a proposal for formal adoption by the committee in discussions about templates, and for the purposes of clarifying the documentation; I propose we adopt the formalisation, that a trailing '-template'describes a set of types or functions described by a template. And that a leading 'template-', is used to describe the template definition of part of a '-template', such as a 'template-member-function'. Thus :-

  • 'function-template':A set of functions described by a template, parametric on some type information provided as argument to that template. For example :-

一致的术语

迄今为止的大部分争论和分歧都与描述模板的章节中使用的术语有关。最常见的是“功能模板”“模板功能”的不同应用来表达不同的想法和意图。由于没有应用一致的命名,结果是混乱和争论。

就本文件而言,作为委员会在讨论模板时正式采用的提案,以及澄清文件的目的;我建议我们采用形式化,即尾随的“-template”描述由模板描述的一组类型或函数。并且前导 'template-'用于描述'-template'的一部分的模板定义,例如'template-member-function'. 因此 :-

  • 'function-template':由模板描述的一组函数,对作为参数提供给该模板的某些类型信息进行参数化。例如 :-
template<class T> int nullcheck( T* pT )
{ return ( pT != 0 ); }
  • 'class-template':A set of classes described by a template, parametric on some type information provided as argument to that template. For Example :-
  • 'class-template':由模板描述的一组类,对作为参数提供给该模板的某些类型信息进行参数化。例如 :-
template<class T> class S {
int i;
public:
int sep_member();
int imm_member()
{ return 2; }
}
  • 'template-function':This term is no longer permitted.**
  • 'template-class':This term is not permitted.**

  • 'member-function-template':This term is not permitted, as it describes a property not currently supported by the template definition. Using the above terminology convention, this would describe a member of a non-class-template, whose definition was itself a template. For example :-

  • “模板功能”:不再允许使用该术语。**
  • 'template-class':不允许使用该术语。**

  • 'member-function-template':不允许使用该术语,因为它描述了模板定义当前不支持的属性。使用上述术语约定,这将描述非类模板的成员,其定义本身就是模板。例如 :-

class Normal { public:
template<class T> int foo(T*pT)
{ return ( pT == 0 ); }
};

However, since templates are currently limited to the global scope, such a template is invalid.

  • 'template-static-member-function':
  • 'template-member-function':
  • 'template-static-member':
  • 'template-static-data-member'template-member':Alternative terms for the definition of a member appearing separate to the 'class-template' to which it belongs. For example :-

但是,由于模板目前仅限于全局范围,因此此类模板无效。

  • “模板静态成员函数”:
  • “模板成员函数”:
  • “模板静态成员”:
  • 'template-static-data-member 'template-member':成员定义的替代术语,与其所属的 'class-template' 分开出现。例如 :-
template<class T> int S<T>::sep_member()
{ return i; }

回答by Rishabh

Template class: A class that has generic definition or a class with parameters which is not instantiated until the information is provided by the client. It is referred to a jargon for plain templates.simply class with prefix template and use of T . Class template: The individual construction of a class is specified by a class template which is almost similar the way how individual objects are constructed by using a class. It is referred to a object of the template class Ex- classname objectname(argument list)

模板类:具有通用定义的类或具有参数的类,在客户端提供信息之前不会实例化。它被称为普通模板的行话。带有前缀模板和使用 T 的简单类。类模板:类的单个构造由类模板指定,这几乎类似于使用类构造单个对象的方式。它引用了模板类的对象 Ex- classname objectname(argument list)

回答by Ayan Kumar Saha

Class template is a generic class for different types of objects. Basically it provides a specification for generating classes based on parameters. Whenever a new object is created a new class will take place in the memory for that purpose. This is called instantiating a class template and each instantiated version of the class is called template class.

类模板是不同类型对象的通用类。基本上它提供了基于参数生成类的规范。每当创建一个新对象时,都会在内存中为此目的创建一个新类。这称为实例化类模板,类的每个实例化版本称为模板类。