3

I read in many articles, that for class template when specializing member template, the class that containing specialized member template also shall be explicitly specialized. Is there a point about it in standard and is there any reason to have such restriction? I mean under the hood.

Why this is not allowed.

template <typename T>
class A
{
   template <typename U>
   void foo()
   {}
};

template <typename T>
 template <>
void A<T>::foo<int>()
{}
  • See [CWG 727](http://www.open-std.org/JTC1/SC22/WG21/docs/cwg_active.html#727) and [N4090](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n4090.pdf) – dyp Jul 04 '15 at 17:42

1 Answers1

1

[temp.expl.spec]/16:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialized, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. [ Example:

template <class T1> class A {
    template<class T2> class B {
        template<class T3> void mf1(T3);
        void mf2();
    };
};

template <> template <class X>
class A<int>::B {
    template <class T> void mf1(T);
};

template <> template <> template<class T>
void A<int>::B<double>::mf1(T t) { }
template <class Y> template <>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized 
                                // but its enclosing class template A is not

end example ]

Columbo
  • 60,038
  • 8
  • 155
  • 203