简体   繁体   English

模板显式专业化的C ++措辞

[英]C++ wording for template explicit specialization

If we consider function template overloading, the standard behaviour in C++ is to first choose the "most specialized" overload (out of the base templates). 如果我们考虑函数模板重载,则C ++中的标准行为是首先选择“最专门的”重载(从基本模板中选择)。 The next step is to see if the chosen overload is explicitly specialized. 下一步是查看所选的过载是否明确专用。 If it is, the explicit specialization that matches will be chosen. 如果是,将选择匹配的显式专业化。

Could you point to the place in the standard that defines the second step (the highlighted part in the previous paragraph)? 您能否指出定义第二步的标准中的位置(上一段中突出显示的部分)?

Thank you. 谢谢。

From the working draft (N4713): 从工作草案(N4713):

17.6.6.2 Partial ordering of function templates [temp.func.order] 17.6.6.2功能模板的部分排序[临时功能顺序]
1. If a function template is overloaded, the use of a function template specialization might be ambiguous because template argument deduction may associate the function template specialization with more than one function template declaration. 1.如果功能模板过载,则使用功能模板专业化可能会模棱两可,因为模板参数推导可能会将功能模板专业化与多个功能模板声明相关联。 Partial ordering of overloaded function template declarations is used in the following contexts to select the function template to which a function template specialization refers: 在以下上下文中,重载函数模板声明的部分排序用于选择函数模板专门化所引用的函数模板:
(1.1) — during overload resolution for a call to a function template specialization; (1.1)—在重载解析期间调用函数模板专门化功能;
(1.2) — when the address of a function template specialization is taken; (1.2)—当采用功能模板专门化的地址时;
(1.3) — when a placement operator delete that is a function template specialization is selected to match a placement operator new; (1.3)—当选择作为功能模板特化的放置运算符delete来匹配new的放置运算符时;
(1.4) — when a friend function declaration, an explicit instantiation or an explicit specialization refers to a function template specialization. (1.4)—当朋友函数声明,显式实例化或显式专门化是指函数模板专门化时。

Also: 也:

2 Partial ordering selects which of two function templates is more specialized than the other by transforming each template in turn (see next paragraph) and performing template argument deduction using the function type. 2 偏序排序通过依次转换每个模板(请参见下一段)并使用函数类型执行模板自变量推导来选择两个功能模板中的哪一个比另一个更为专业。 The deduction process determines whether one of the templates is more specialized than the other . 推导过程确定其中一个模板是否比另一个模板更专业 If so, the more specialized template is the one chosen by the partial ordering process. 如果是这样,则更专业的模板是部分订购过程选择的模板。 If both deductions succeed, the partial ordering selects the more constrained template as described by the rules in 17.4.4. 如果两个推论都成功,则偏序选择约束更严格的模板,如17.4.4中的规则所述。

17.6.5.2 Partial ordering of class template specializations [temp.class.order] 17.6.5.2类模板专业化的部分排序[temp.class.order]
1 For two class template partial specializations, the first is more specialized than the second if, given the following rewrite to two function templates, the first function template is more specialized than the second according to the ordering rules for function templates (17.6.6.2): (1.1) — Each of the two function templates has the same template parameters and associated constraints (17.4.2) as the corresponding partial specialization. 1对于两个类模板的部分专业化,如果在以下重写为两个功能模板的情况下,根据功能模板的排序规则(17.6.6.2),第一个功能模板比第二个更专业,则第一个要比第二个专业。 :(1.1)—两个功能模板中的每一个都具有与相应的部分专业化相同的模板参数和关联的约束(17.4.2)。
(1.2) — Each function template has a single function parameter whose type is a class template specialization where the template arguments are the corresponding template parameters from the function template for each template argument in the template-argument-list of the simple-template-id of the partial specialization . (1.2)— 每个函数模板都有一个函数参数,其类型是类模板的特殊化,其中模板参数是简单模板ID的template-argument-list中每个模板参数对应的函数模板中的模板参数部分专业化
2 [ Example: 2 [示例:

 template<int I, int J, class T> class X { }; template<int I, int J> class X<I, J, int> { }; // #1 template<int I> class X<I, I, int> { }; // #2 template<int I0, int J0> void f(X<I0, J0, int>); // A template<int I0> void f(X<I0, I0, int>); // B template <auto v> class Y { }; template <auto* p> class Y<p> { }; // #3 template <auto** pp> class Y<pp> { }; // #4 template <auto* p0> void g(Y<p0>); // C template <auto** pp0> void g(Y<pp0>); // D 

According to the ordering rules for function templates, the function template B is more specialized than the function template A and the function template D is more specialized than the function template C. Therefore, the partial specialization #2 is more specialized than the partial specialization #1 and the partial specialization #4 is more specialized than the partial specialization #3. 根据功能模板的排序规则,功能模板B比功能模板A更专业,功能模板D比功能模板C更专业。因此,部分专业化#2比部分专业化#更专业。在图1中,局部专业化#4比局部专业化#3更专业。 —end example ] —结束示例]

[ Example: [示例:

 template<typename T> concept C = requires (T t) { tf(); }; template<typename T> concept D = C<T> && requires (T t) { tf(); }; template<typename T> class S { }; template<C T> class S<T> { }; // #1 template<D T> class S<T> { }; // #2 template<C T> void f(S<T>); // A template<D T> void f(S<T>); // B 

The partial specialization #2 is more specialized than #1 because B is more specialized than A . 部分专业化#2比#1更专业,因为B比A更专业 —end example ] —结束示例]

If I understand you correctly then perhaps you were referring to this from [temp.inst§4] : 如果我对您的理解正确,那么也许您是指[temp.inst§4]中的内容

Unless a function template specialization has been explicitly instantiated or explicitly specialized, the function template specialization is implicitly instantiated when the specialization is referenced in a context that requires a function definition to exist or if the existence of the definition affects the semantics of the program [...] 除非已明确实例化或明确指定了功能模板特化,否则在需要存在函数定义的上下文中引用该特时,或者如果定义的存在影响程序的语义,则将隐式实例化功能模板特化 。 ..]

Which, by way of negation, says that the explicit specialization gets the precedence. 通过否定,它表示显式专业化具有优先权。

The standard does not define formally what an explicit specialization is. 该标准没有正式定义什么是显式专业化。 The most relevant part I can find is the example in [temp.expl.spec]/1: 我可以找到的最相关的部分是[temp.expl.spec] / 1中的示例

[ Example: [示例:

 template<class T> class stream; template<> class stream<char> { /* ... */ }; template<class T> class Array { /* ... */ }; template<class T> void sort(Array<T>& v) { /* ... */ } template<> void sort<char*>(Array<char*>&); 

Given these declarations, stream<char> will be used as the definition of streams of char s; 给定这些声明, stream<char>将用作char的流的定义; other streams will be handled by class template specializations instantiated from the class template. 其他流将由从类模板实例化的类模板专业化处理。 Similarly, sort<char*> will be used as the sort function for arguments of type Array<char*> ; 同样, sort<char*>将用作Array<char*>类型的参数的排序函数; other Array types will be sorted by functions generated from the template. 其他Array类型将根据模板生成的函数进行排序。 — end example ] —结束示例]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM