W.F. W.F. - 10 months ago 42
C++ Question

Why is in-class partial specialization well-formed?

According to [temp.class.spec] 5/ (emphasis mine)

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

This would suggest that partial specialization (just like in case of explicit specialization) have to appear in the namespace scope. This is actually confirmed by the example below the paragraph:

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

On the other hand C++ Standard Core Language Active Issues No 727 example suggests that in-class partial specialization is well formed:

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

I'm sure core issues document is correct here, but cannot find appropriate reference to confirm that. Can you help me?

Answer Source

This is either underspecified or defective, because the intent is that it is valid—see 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)

I filed a core issue.