在哪里声明类成员模板的部分特化? [英] Where to declare partial specializations of class member templates?

查看:101
本文介绍了在哪里声明类成员模板的部分特化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里stackoverflow我发现几个评论(例如, jrok 问题),说明在非命名空间范围允许类成员模板的部分专门化(相反如下例所示:

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:


类模板部分特化可以在任何可以定义其定义的命名空间范围内声明或重新声明(14.5.1和14.5.2)。

A class template partial specialization may be declared or redeclared in any namespace scope in which its definition may be defined (14.5.1 and 14.5.2).

与以下示例一起:

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:


如果类模板的成员模板是部分专用的,
成员模板部分专业化是
封装类模板的成员模板; [...] [例如:

If a member template of a class template is partially specialized, the member template partial specializations are member templates 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 $ 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.

另请参阅核心语言问题708 EWG Issue 41

这篇关于在哪里声明类成员模板的部分特化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆