CTTCG 10 Basic Template Terminology

Class Template v.s. Template Class

前者是生成类的模板,后者既可以指模板,又可以指从模板生成的类。

Substitution, Instantiation, and Specialization

  • 参数替代是模板生成相应对象(即实例化)的一个中间环节。
  • 实例化是从模板生成相应对象的动作。
  • 特化是和模板形式相似但匹配程度更高的对象。特化分为实例化(生成式特化)和显式特化(explicit specialization,即人为提供特化函数)。

Declarations versus Definitions

函数、类或变量等有了名字,但是定义不完整,就算作声明。

对变量初始化,或者缺少 extern 修饰符(specifier)时,声明就变成了定义。

The One-Definition Rule(ODR)

  • Ordinary (i.e., not templates) noninline functions and member functions, as well as (noninline) global variables and static data members should be defined only once across the whole program. 非内联函数和成员函数、(非内联的)全局和(非内联的)静态变量在整个程序中只能定义一次。
  • Class types (including structs and unions), templates (including partial specializations but not full specializations), and inline functions and variables should be defined at most once per translation unit, and all these definitions should be identical. 类和不完整的模板、内联函数和变量每个翻译单元中最多定义一次,整个程序中的多个定义必须完全相等。

Global and static variables and data members can be defined as inline since C++17. This removes the requirement that they be defined in exactly one translation unit. C++17 之后全局和静态变量是可以用 inline 修饰的。

Template Arguments versus Template Parameters

前者指实参,后者指形参。另外 template-id 指的是 模板名<Arg1, Arg2>