问题描述
这里stackoverflow我发现几个评论(例如,在问题),说明在非命名空间范围允许类成员模板的部分专门化(相反如下例所示:
Here on stackoverflow I've found several comments (see for example the comment by jrok on this question) stating that partial specializations of class member templates are allowed at non-namespace scope (in contrast to explicit specializations) like in the example below:
class A {
template <class T, class U>
class B {};
template <class U>
class B<void, U> {};
};
此外,本示例使用gcc和clang进行编译。但是在c ++ 03标准文本中,我只能找到关于这个问题的14.5.4 [temp.class.spec]§6(或者14.5.5§5):
Also, this example compiles just fine with both gcc and clang. However in the c++03 standard text I can only find 14.5.4 [temp.class.spec] §6 (or 14.5.5 §5 in c++11) about this issue:
与以下示例一起:
template<class T> struct A {
class 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*> { };
那么,在非命名空间范围的类模板部分特化他们是否被标准允许?
So, how about class template partial specializations at non-namespace scope? Are they allowed by the standard? And where can I find the relevant text?
具体来说,我的示例是有效的(如果封闭类是模板,它仍然有效)?
Specifically, is my example valid (and would it still be valid if the enclosing class would be a template)? If not, are current compilers wrong to compile my example as stated above?
推荐答案
这似乎是有点混乱,因为我同意您引用的段落似乎禁止在类定义内部的部分专门化。但是,有[temp.class.spec.mfunc] / 2:
This seems to be a bit confusing, as I agree the paragraph you quoted seems to disallow partial specializations inside class definitions. However, there's [temp.class.spec.mfunc]/2:
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 $ b
— end example ]
IMHO这不是很明确;它不会在类定义内允许部分专门化,而是(我似乎)指定如何处理成员模板特化。
IMHO this isn't very explicit; it doesn't allow partial specialization inside the class definition but rather (to me, seems to) specifies how member template specializations are treated.
另请参阅和。
这篇关于在哪里声明类成员模板的部分特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!