【发布时间】:2016-11-17 14:00:01
【问题描述】:
根据 [temp.class.spec] 5/(强调我的)
可以声明或重新声明类模板的部分特化 在相应的主模板可以在其中的任何命名空间范围中 被定义
这表明部分特化(就像显式特化一样)必须出现在命名空间范围内。这实际上已被该段下方的示例所证实:
template<class T> struct A {
struct C {
template<class T2> struct B { };
};
};
// partial specialization of A<T>::C::B<T2>
template<class T> template<class T2>
struct A<T>::C::B<T2*> { };
//...
A<short>::C::B<int*> absip; // uses partial specialization
另一方面,C++ Standard Core Language Active Issues No 727 示例表明类内部分特化是良好的:
struct A {
template<class T> struct B;
template <class T> struct B<T*> { }; // well-formed
template <> struct B<int*> { }; // ill-formed
};
我确定这里的核心问题文档是正确的,但找不到合适的参考来确认这一点。你能帮帮我吗?
【问题讨论】:
-
"模板可以在类或类模板中声明;这样的模板称为成员模板。"。回想一下,部分特化本身就是模板。
-
@JohannesSchaub-litb 但是为什么它不适用于显式专业化呢?它们不也是模板吗?
-
你想通了。它们不是模板。它们是类或函数或变量。
-
@JohannesSchaub-litb 这是有道理的...如果您找到合适的规范片段,我会接受答案
-
@JohannesSchaub-litb 类、函数和变量也可以在类模板中声明,不是吗?
标签: c++ templates language-lawyer template-specialization partial-specialization