c++ - 显式特化的成员也需要其包含的类模板也被显式特化

标签 c++ templates member explicit-specialization

我读过很多文章,专门针对类模板 成员模板,包含专门成员模板的类也应明确专门化。标准中是否有关于它的观点,是否有任何理由有这样的限制? 我的意思是在幕后。

为什么不允许这样做。

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/

相关文章:

c++ - 如何在 XCode 4.2 中一起使用 c++ 和 objective-c

c++ - 如何检测USB设备的 "Over Current"事件?

c++ - 是否可以创建一个函数来比较从对象 vector 中动态选择的变量?

c++ - 使用模板模板参数打破模板循环依赖

c++ - 如何专门化类模板成员函数?

c++ - 对象成员和标准类型的模板函数

c++ - 如何将非静态成员函数作为回调传递?

c++ - 如何只读入特定数量的字符

c++ - 如何只链接到 Conan 包中的某些库?

c++ - 模板是否具有范围或范围之类的东西