我读过很多文章,专门针对类模板 成员模板,包含专门成员模板的类也应明确专门化。标准中是否有关于它的观点,是否有任何理由有这样的限制? 我的意思是在幕后。
为什么不允许这样做。
template <typename T>
class A
{
template <typename U>
void foo()
{}
};
template <typename T>
template <>
void A<T>::foo<int>()
{}
最佳答案
[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 ]
关于c++ - 显式特化的成员也需要其包含的类模板也被显式特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909185/