首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Class template

类模板定义了一系列的类。

句法

template < parameter-list > class-declaration

(1)

?

export template < parameter-list > class-declaration

(2)

(until C++11)

解释

class-declaration

-

a class declaration. The class name declared becomes a template name.

parameter-list

-

a non-empty comma-separated list of the template parameters, each of which is either a non-type parameter, a type parameter, a template parameter, or a parameter pack of any of those.

export was an optional modifier which declared the template as exported (when used with a class template, it declared all of its members exported as well). Files that instantiated exported templates did not need to include their definitions: the declaration was sufficient. Implementations of export were rare and disagreed with each other on details.

(until C++11)

类模板实例化

类模板本身不是类型、对象或任何其他实体。不从仅包含模板定义的源文件生成任何代码。为了使任何代码出现,必须实例化模板:必须提供模板参数,以便编译器可以从函数模板%29生成实际的类%28或函数。

显式实例化

template class name < argument-list >?;

(1)

?

extern template class name < argument-list >?;

(2)

(since C++11)

1%29显式实例化定义

2%29显式实例化声明

显式实例化定义强制对它们所引用的类、结构或联合进行实例化。它可能出现在程序的模板定义之后的任何地方,对于给定的参数列表,只允许在整个程序中出现一次。

An explicit instantiation declaration (an extern template) skips implicit instantiation step: the code that would otherwise cause an implicit instantiation instead uses the explicit instantiation definition provided elsewhere (resulting in link errors if no such instantiation exists). This can be used to reduce compilation times by explicitly declaring a template instantiation in all but one of the source files using it, and explicitly defining it in the remaining file.

(since C++11)

类、函数、变量和成员模板专门化可以从它们的模板中显式实例化。可以从类模板的成员定义显式实例化类模板的成员函数、成员类和静态数据成员。

显式实例化只能出现在模板的封闭名称空间中,除非它使用了限定-id:

二次

代码语言:javascript
复制
namespace N {
  template<class T> class Y { void mf() { } }; // template definition
}
// template class Y<int>; // error: class template Y not visible in the global namespace
using N::Y;
// template class Y<int>; // error: explicit instantiation outside 
                          // of the namespace of the template
template class N::Y<char*>;      // OK: explicit instantiation
template void N::Y<double>::mf(); // OK: explicit instantiation

二次

显式实例化在显性专业化出现在相同的模板参数集之前。

只有在显式实例化函数模板、变量模板、类模板的成员函数或静态数据成员或成员函数模板时,才需要声明可见。必须在类模板、类模板的成员类或成员类模板的显式实例化之前出现完整的定义,除非前面出现具有相同模板参数的显式专门化。

如果使用显式实例化定义显式实例化类模板的函数模板、变量模板、成员函数模板或成员函数或静态数据成员,则模板定义必须存在于同一转换单元中。

当显式实例化命名类模板专门化时,它充当一个显式实例化,其类型为%28声明或定义,其每个非继承的非模板成员中,有%29是以前在翻译单元中没有显式专门化的。如果这个显式实例化是一个定义,它也是一个显式实例化定义,仅针对此时定义的成员。

显式实例化定义忽略成员访问说明符:参数类型和返回类型可能是私有的。

隐实例化

当代码引用需要完全定义类型的上下文中的模板时,或者当类型的完整性影响到代码时,并且该特定类型尚未显式实例化,则会发生隐式实例化。例如,当构造此类型的对象时,而不是在构造指向此类型的指针时。

这适用于类模板的成员:除非在程序中使用成员,否则不实例化,不需要定义。

二次

代码语言:javascript
复制
template<class T> struct Z {
    void f() {}
    void g(); // never defined
}; // template definition
template struct Z<double>; // explicit instantiation of Z<double>
Z<int> a; // implicit instantiation of Z<int>
Z<char>* p; // nothing is instantiated here
p->f(); // implicit instantiation of Z<char> and Z<char>::f() occurs here.
// Z<char>::g() is never needed and never instantiated: it does not have to be defined

二次

如果在实例化时已声明了类模板,但未定义该类模板,则实例化将生成一个不完整的类类型:

二次

代码语言:javascript
复制
template<class T> class X; // declaration, not definition
X<char> ch;                // error: incomplete type X<char>

二次

Local classes and any templates used in their members are instantiated as part of the instantiation of the entity within which the local class or enumeration is declared.

(since C++17)

另见

  • 模板参数和参数允许模板参数化
  • 函数模板声明声明函数模板
  • 模板专业化为特定类型定义现有模板。
  • 参数包允许使用模板%28中的类型列表,因为C++11%29
代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com