ISO/IEC 14882 2017 5edition个人翻译 17 模板

写在开头:
这个系列的翻译会不定时跟进,因为是为了找出Cpperference中没有看懂的地方而做的标准翻译,不会一次性翻译某一块内容,只能说看到哪翻译到哪,下次继续…
由于本人水平有限翻译的不恰当(错误)的地方还请指出!谢谢!
要讨论的同学,老师们可以直接在评论区留言(本人不经常逛CSDN可能回复不会很即时请见谅)
更快的联系方式:可以移步我的cpperference的讨论页中留言或是在讨论页的签名处有直接联系方式
最后谢谢各位看客来看本人的撇脚翻译!

ISO/IEC 14882 2017 5edition个人翻译

17 Templates [TEMP]

介绍

1

A template defines a family of classes, functions, or variables, or an alias for a family of types.
template-declaration :
    template < template-parameter-list > declaration
template-parameter-list :
    template-parameter
    template-parameter-list , template-parameter


一个模板定义了一个类,函数,变量的族或是一个类型族的别名
模板声明形式为:
template <模板参数序列> 声明
模板参数序列为:
    模板参数
    模板参数序列,模板参数


[ Note: The > token following the template-parameter-list of a template-declaration may be the product of replacing a >> token by two consecutive > tokens (17.2) . — end note ]


[注记:在模板声明的模板参数列表后的>记号可能是用两个连续的>记号来构成的>>来替换]


The declaration in a template-declaration shall
(1. 1)— declare or define a function, a class, or a variable, or
(1. 2)— define a member function, a member class, a member enumeration, or a static data member of a class template or of a class nested within a class template, or
(1. 3)— define a member template of a class or class template, or
(1. 4)— be a deduction-guide, or
(1. 5)— be an alias-declaration.
A template-declaration is a declaration . A template-declaration is also a definition if its declaration defines a
function, a class, a variable, or a static data member. A declaration introduced by a template declaration of
a variable is a variable template . A variable template at class scope is a static data member template .


模板声明中的声明应该是:
(1.1)—定义或声明了一个函数或者类或者变量
(1.2)—定义了一个成员函数,一个成员类,一个成员枚举项或是一个模板类或一个嵌套在模板类的类的静态成员
(1.3)—定义了类或模板类的成员模板类
(1.4)—作为一个deduction-guide(这个是C++17的新特性,后面的翻译会有(也许把我先解决完当前的hhh))
(1.5)—作为一个别名声明(using … = …; C++11)
一个模板声明是一个声明
当在模板声明中的的声明处定义了一个函数,类,变量或是一个静态数据成员这个模板声明同样也是定义
一个被变量模板声明引入的声明是变量模板
一个类内的变量模板是一个静态成员模板


例子:
template<class T>
constexpr T pi = T(3. 1415926535897932385L) ;
template<class T>
T circular_area(T r) {
return pi<T> * r * r;
}
struct matrix_constants {
template<class T>
using pauli = hermitian_matrix<T, 2>;
template<class T>
constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } };
template<class T>
constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };
template<class T>
constexpr pauli<T> sigma3 = { { 1, 0 }, { 0, -1 } };
};


2

A template-declaration can appear only as a namespace scope or class scope declaration. In a function template declaration, the last component of the declarator-id shall not be a template-id . [ Note: That last component may be an identifier, an operator-function-id , a conversion-function-id, or a literal-operator-id . In a class template declaration, if the class name is a simple-template-id, the declaration declares a class template partial specialization (17.5.5) . — end note ]


一个模板声明可以只在一个名称空间或类声明空间内出现,在函数模板声明中,声明器-id中最后的一个组成成份不会是一个template-id(在17.2中)
< Kaslana注:
注意在标准文档中有提到:
X-id is an identifier with no context-dependent meaning
就是说:X-id就表明是一个没有上下文意义的标识符
相关标准文献通用原则请移步{ToDo 翻译第四节《 General principles 》}
end>
[注意:最后一个组成成份可能是一个标识符,一个重载函数标识符或是一个用户定义字面量标识符,在一个类模板声明中,如果类名是一个simple-template-id(模板名是模板参数,或者模板参数是依赖的类型或是依赖类型或是依赖值的表达式或包)那么这个声明声明了一个偏特化模板]


3

In a template-declaration, explicit specialization, or explicit instantiation the init-declarator-list in the declaration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.


在一个模板声明中,发生显式特例化或显式实例化那么在声明中可带初始化的声明器序列应该包含至少一个声明器,当一个声明被用于声明一个类模板,那么没有一类声明器是被允许使用的


4

A template name has linkage (6.5) . Specializations (explicit or implicit) of a template that has internal linkage are distinct from all specializations in other translation units. A template, a template explicit specialization (17.7.3) , and a class template partial specialization shall not have C linkage. Use of a linkage specification other than ” C” or ” C++” with any of these constructs is conditionally-supported, with implementation-defined semantics. Template definitions shall obey the one-definition rule (6.2) . [ Note: Default arguments for function templates and for member functions of class templates are considered definitions for the purpose of template instantiation (17.5) and must also obey the one-definition rule. — end note ]


一个模板名会被连接。一个模板的显式或隐式特例化具有从其他所有翻译单元中的的特例化不同的内部连接。一个模板,一个模板显式特例化和一个类模板偏特化不应该具有C的链接方式。在其他一些编译器结构中使用一个除了“C”或“C++”的链接指示符或是使用与实现定义的语句是有条件地被支持的(被编译器特殊支持)。模板定义应该遵循单一定义规则。[注意:对函数模板或类模板的成员函数使用默认参数是被认为用于模板势力的定义并且必须遵循单一定义原则]


5

A class template shall not have the same name as any other template, class, function, variable, enumeration, enumerator, namespace, or type in the same scope (6.3) , except as specified in 17.5.5. Except that a function template can be overloaded either by non-template functions (11.3.5) with the same name or by other function templates with the same name (17.8.3) , a template name declared in namespace scope or in class scope shall be unique in that scope.


类模板在一个命名空间内不应该有和其他模板,类,函数,变量,枚举项,枚举,命名空间或类型同名,除了在17.5.5中的特例即函数模板,函数模板既可以被同名的函数模板重载也可以被非模板函数重载,一个在名称空间或类空间内的模板声明应该在该空间内是独一无二的


6

A templated entity is
(6. 1) — a template,
(6. 2) — an entity defined (6.1) or created (15.2) in a templated entity,
(6. 3) — a member of a templated entity,
(6. 4) — an enumerator for an enumeration that is a templated entity, or
(6. 5) — the closure type of a lambda-expression (8.1.5.1) appearing in the declaration of a templated entity.
[ Note: A local class, a local variable, or a friend function defined in a templated entity is a templated entity.
— end note ]


模板实体可以是:
一个模板
一个被定义或声明于模板实体内的一个实体
模板实体的成员
模板内枚举的枚举项
出现在模板实体声明中的lambda表达式闭包类型
[注意:一个内部类,内部变量,或是定义在模板实体内的友元函数是一个模板实体]
< Kaslana注:
个人理解:
消除歧义,因为模板实体会随着模板实例化经过实例化过程一同被实例化,但是作为模板实体的类模板非成员函数在C++ Primer中说明了是被使用才将其实例化的,此处并不存在歧义,因为实例化过程发生在第三翻译阶段之后的,第三翻译阶段以及保留了预处理记号,并且有的编译器在第八翻译阶段同样会检查需要实例化的成员,所以此处的实例化仍然是一同进行的 >


7

A function template, member function of a class template, variable template, or static data member of a class template shall be defined in every translation unit in which it is implicitly instantiated (17.7.1) unless the corresponding specialization is explicitly instantiated (17.7.2) in some translation unit; no diagnostic is required.


一个函数模板,类模板的成员函数,变量模板或是类模板的静态成员应该在每个隐式实例化的翻译单元内被定义< Kaslana注:即定义可见 >除非在一些翻译单元中相应的特例化被显式实例化,不需要判断其合法性< diagnostic>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值