[英]C++ explicit specialization of member template belonging to template class
In the current C++ standard draft, there is this example in this paragraph belonging to the section related to explicit specialization of templates: 在当前的C ++标准草案中, 本段中的这个示例属于与模板的显式专业化相关的部分:
template<class T> struct A {
void f(T);
template<class X1> void g1(T, X1);
template<class X2> void g2(T, X2);
void h(T) { }
};
// specialization
template<> void A<int>::f(int);
// out of class member template definition
template<class T> template<class X1> void A<T>::g1(T, X1) { }
// member template specialization
template<> template<class X1> void A<int>::g1(int, X1); //(1)
// member template specialization
template<> template<>
void A<int>::g1(int, char); //(2)
In (1) it seems more like g1
was specialized to be still a function template in the specialized version of A (A< int >), while in (2) it seems like g1
is itself specialized for its own set template parameters ( (int, from A< int >), char). 在(1)中,似乎
g1
被专门化为仍然是A的专用版本(A <int>)中的函数模板,而在(2)中,似乎g1
本身专门针对其自身的设置模板参数(( int,来自A <int>),char)。
I find there to be a difference between these specializations (again, (1) feels like declaring a new version of g1
to be used for a "special version" of its "container" A
, while (2) feels like a specialization regarding g1
itself (or regarding its own template parameters). 我发现这些专业化之间存在差异(再次,(1)感觉像是声明要用于其“容器”
A
的“特殊版本”的g1
的新版本,而(2)感觉像是关于g1
的专业化本身(或关于其自身的模板参数)。
Furthermore, consider this example: 此外,请考虑以下示例:
template<class T> struct A{
int f() { return 1; }
}
template<>
int A<int>::f() { return 2; } //(3)
To me (1) and (3) are the same "kind of specialization", one that is linked to a special version of the "container", while (2) is a specialization of the entity (template) itself. 对我来说(1)和(3)是相同的“一种特殊化”,一种链接到“容器”的特殊版本,而(2)是实体(模板)本身的一种特殊化。
Does the standard mention this difference or are these two kinds of specialization referred to as the same? 标准中是否提到了这种差异,还是这两种专业化被称为相同?
Thank you. 谢谢。
First, your #3 is equivalent to the first specialization in the cited example, except that the standard's f
uses the class's template parameter in its signature—presumably to illustrate that, since the signature must match for a specialization, template arguments for the class may need to be repeated in the specialization declaration. 首先,您的#3等同于引用的示例中的第一个专业化,除了标准的
f
在其签名中使用类的template参数外-大概是为了说明,由于签名必须与专业化匹配,因此该类的模板参数可能需要在专业声明中重复。
Then, the difference is that #1 is a specialization of a member of A<T>
when T
is int
—a sort of shorthand for writing a specialization of A<int>
itself that is mostly the same as the primary template. 然后,不同之处在于,当
T
为int
时,#1是A<T>
的成员的特化—一种写A<int>
本身的特化的简写,与主要模板大体相同。 In particular, to avoid being misleading, the signature for the member being specialized must be unchanged from the (instantiation of the) primary template. 特别是,为了避免引起误解,要对专门成员的签名必须与主模板(实例化)保持不变。 In this case, that means it's still a template.
在这种情况下,这意味着它仍然是模板。
On the other hand, #2 is a specialization of a template that happens to be a member of A<int>
—the specialization is of A<int>::g1
itself, not of “ A<T>::g1
when T
is int
” as for #1. 在另一方面,#2是恰好是成员的模板的特
A<int>
-the专业化是的A<int>::g1
不是“本身, A A<T>::g1
时T
是int
”作为#1。 This of course applies only when the member is a template, unlike #3. 当然,仅当成员是模板时才适用,这与#3不同。 (In this case, #2 is a specialization of the very template declared by #1!)
(在这种情况下,#2是#1声明的模板的特殊化!)
Part of the point of [temp.expl.spec]/15 is that these two cases are not strongly distinguished syntactically. [temp.expl.spec] / 15的部分要点是,这两种情况在语法上没有明显区别。 The difference is largely academic: both are surgical changes to a templated entity for certain arguments.
区别主要是学术上的:两者都是针对某些论点的模板实体的外科手术更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.