为什么 in-class 偏特化是良构的?

Why is in-class partial specialization well-formed?

根据 [temp.class.spec] 5/(强调我的)

A class template partial specialization may be declared or redeclared in any namespace scope in which the corresponding primary template may be defined

这表明部分特化(就像显式特化一样)必须出现在命名空间范围内。段落下方的示例实际上证实了这一点:

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 示例表明 in-class 偏特化的结构良好:

struct A {
  template<class T> struct B;
  template <class T> struct B<T*> { }; // well-formed
  template <> struct B<int*> { }; // ill-formed
};

我确定此处的核心问题文档是正确的,但找不到合适的参考来确认这一点。你能帮帮我吗?

目的是它是有效的——见N4090:

Following a brief discussion of DR 17557 and DR 7278 in Issaquah 2014, and based on discussion on the core-reflector91011, it seems as if Core is converging on the following rules for member templates and their specializations: Partial specializations and explicit specializations can be first declared at either innermost-enclosing-class scope or enclosing namespace scope (recognizing that explicitly declaring specializations does not constitute adding members to a class and hence can be done after the closing brace).

7 http://www.open­std.org/jtc1/sc22/wg21/docs/cwg_toc.html#727
8 http://www.open­std.org/jtc1/sc22/wg21/docs/cwg_toc.html#1755
9 http://accu.org/cgi­bin/wg21/message?wg=core&msg=24366(24033, 24290, 24309, 24368)
10 http://accu.org/cgi­bin/wg21/message?wg=core&msg=24731(24731, 24732, 24736, 24738)
11 http://accu.org/cgi­bin/wg21/message?wg=core&msg=25168 (25168­-25179)

我提了一个core issue,因为感觉现在的表述不够清晰;您引用的段落可以解释为不允许 in-class 部分专业化。

规范说在 14.5.2p1

A template can be declared within a class or class template; such a template is called a member template.

并在 14.5.5p2

Each class template partial specialization is a distinct template and definitions shall be provided for the members of a template partial specialization

因此,class模板部分特化是一个模板,这也是自然的,因为它仍然有不固定的参数,因此它表示"family of classes"。模板可以在 class 或 class 模板中声明。