c++ - 使用给定类对类模板进行特化

标签 c++ templates template-specialization

我有一个类 A 和一个类模板 B 声明如下:

class A;

template <class T>
class B;

我想为 T=int 声明 B 的特化,它与 A 重合,即像这样

template<>
class B<int> = A;

最佳答案

您可以通过嵌套类和 C++11 模板别名模拟 B 的这种行为:

class A;

template <class T>
struct B_
{
    class type{ /* Implement your general version of B here */ };
};

template <>
struct B_<int>
{
    using type = A;
};

template <class T>
using B = typename B_<T>::type;

Live demo

关于c++ - 使用给定类对类模板进行特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265212/

相关文章:

angularjs - Chrome 打包应用程序 AngularJS 样板模板

c++ - 来自可变模板的模棱两可的成员请求

c++ - 在运行时选择合适的专用模板

c++ - 是否可以使用 std::enable_if 来选择成员模板特化?

C++ 虚拟方法的部分模板特化

c++ - 遍历任意形状的点

c++ - 为什么派生类可以调用基类的成员函数?

c++ - 错误 C2228 : left of '.Alloc' must have class/struct/union

c++ - Thrift:是否可以仅使用 C++ Thrift 库进行序列化?

C++ 递归模板分辨率 : flattening vectors of vectors elegantly