模板的隐式特化是什么意思?
What does template's implicit specialization mean?
在第 N3797::14/4 [temp]
章中(强调我的)
A template name has linkage (3.5). A non-member function template can
have internal linkage; any other template name shall have external
linkage. Specializations (explicit or implicit) of a template that
has internal linkage are distinct from all specializations in other
translation units.
提到了隐式专业化。据我从上下文中理解,该概念不同于具有
的模板显式专业化
template < >
声明
语法。所以,我猜测隐式特化与部分 class 模板特化有关。不幸的是,我无法在当前工作草案中规范引用定义隐式专业化概念。
该术语在标准中使用不多,但我们可以从 §14.5.5.3 中推断出它的含义 - 转载如下 - 我将其分为 (A)、(B) 和 (C) 段为了便于参考(粗体我的):
(A) If a member template of a class template is partially specialized, the member template partial specializations are member templates of the enclosing class template; if the enclosing class template is instantiated (14.7.1, 14.7.2), a declaration for every member template partial specialization is also instantiated as part of creating the members of the class template specialization.
(B) If the primary member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the partial specializations of the member template are ignored for this specialization of the enclosing class template.
(C) If a partial specialization of the member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the primary member template and its other partial specializations are still considered for this specialization of the enclosing class template. [ Example:
template<class T> struct A {
template<class T2> struct B {}; // #1
template<class T2> struct B<T2*> {}; // #2
};
template<> template<class T2> struct A<short>::B {}; // #3
A<char>::B<int*> abcip; // uses #2
A<short>::B<int*> absip; // uses #3
A<char>::B<int> abci; // uses #1
-- end example]
重复 (B) 以及我对括号中示例的交叉引用:
"if the primary member template (i.e. #1) is explicitly specialised (as at #3) for a given (implicit) specialisation of the enclosing class template (A
), the partial specialisations of the member template (#2) are ignored for this specialisation of the enclosing class template".
我们看到 #3 的专业化导致 absip;
忽略了 #2。因此我们可以得出结论,以下行...
template<> template<class T2> struct A<short>::B {}; // #3
...执行封闭的 class 模板的隐式特化,即 A<short>
.
因此,隐式特化 是指成员函数的特化隐式涉及 class 模板的特化,它是成员。换句话说,class 模板 A
在 #1 的成员模板专门化之前不需要对 short
进行单独的早期专门化,因为它可以隐式专门化。
没有叫"implicit specialization"的规范术语。
但是,我相信在这种情况下,它意味着 "explicit specialization" 的补充:用户未明确指定的每个专业化,换句话说, 实例化专业化 .
考虑到有
- 明确的专业
- 隐式实例化
- 显式实例化
通过后两者实例化的特化可以称为"implicit specializations"。
在第 N3797::14/4 [temp]
章中(强调我的)
A template name has linkage (3.5). A non-member function template can have internal linkage; any other template name shall have external linkage. Specializations (explicit or implicit) of a template that has internal linkage are distinct from all specializations in other translation units.
提到了隐式专业化。据我从上下文中理解,该概念不同于具有
的模板显式专业化template < >
声明
语法。所以,我猜测隐式特化与部分 class 模板特化有关。不幸的是,我无法在当前工作草案中规范引用定义隐式专业化概念。
该术语在标准中使用不多,但我们可以从 §14.5.5.3 中推断出它的含义 - 转载如下 - 我将其分为 (A)、(B) 和 (C) 段为了便于参考(粗体我的):
(A) If a member template of a class template is partially specialized, the member template partial specializations are member templates of the enclosing class template; if the enclosing class template is instantiated (14.7.1, 14.7.2), a declaration for every member template partial specialization is also instantiated as part of creating the members of the class template specialization.
(B) If the primary member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the partial specializations of the member template are ignored for this specialization of the enclosing class template.
(C) If a partial specialization of the member template is explicitly specialized for a given (implicit) specialization of the enclosing class template, the primary member template and its other partial specializations are still considered for this specialization of the enclosing class template. [ Example:
template<class T> struct A {
template<class T2> struct B {}; // #1
template<class T2> struct B<T2*> {}; // #2
};
template<> template<class T2> struct A<short>::B {}; // #3
A<char>::B<int*> abcip; // uses #2
A<short>::B<int*> absip; // uses #3
A<char>::B<int> abci; // uses #1
-- end example]
重复 (B) 以及我对括号中示例的交叉引用:
"if the primary member template (i.e. #1) is explicitly specialised (as at #3) for a given (implicit) specialisation of the enclosing class template (
A
), the partial specialisations of the member template (#2) are ignored for this specialisation of the enclosing class template".
我们看到 #3 的专业化导致 absip;
忽略了 #2。因此我们可以得出结论,以下行...
template<> template<class T2> struct A<short>::B {}; // #3
...执行封闭的 class 模板的隐式特化,即 A<short>
.
因此,隐式特化 是指成员函数的特化隐式涉及 class 模板的特化,它是成员。换句话说,class 模板 A
在 #1 的成员模板专门化之前不需要对 short
进行单独的早期专门化,因为它可以隐式专门化。
没有叫"implicit specialization"的规范术语。
但是,我相信在这种情况下,它意味着 "explicit specialization" 的补充:用户未明确指定的每个专业化,换句话说, 实例化专业化 .
考虑到有
- 明确的专业
- 隐式实例化
- 显式实例化
通过后两者实例化的特化可以称为"implicit specializations"。