c++ - 部分模板模板特化

标签 c++ templates partial-specialization template-templates

有这个代码:

template<typename T, template<typename, typename> class OuterCont, template<typename, typename> class InnerCont, class Alloc=std::allocator<T>>
class ContProxy { 
    OuterCont<T, InnerCont<T, Alloc>> _container;
};
typedef ContProxy<int, std::vector, std::list> IntCont;

但需要用到T*而不是 std::list<T>作为InnerCont在某些情况下——像这样:

template<typename T, template<typename, typename> class OuterCont, T*, class Alloc=std::allocator<T>>
class ContProxy { 
    OuterCont<T, T*> _container;
};

在这种情况下是否可以使用“模板模板”参数的部分特化?
或者如何以最少的麻烦将其存档..

最佳答案

简单地根据类型 制作模板通常更容易。您无法使用模板模板真正捕获所有情况——如果有人想使用具有六个模板参数的容器怎么办?所以尝试这样的事情:

template <typename T, typename C>
struct ContProxy
{
    typedef C                    container_type;
    typedef typename C::second_type second_type;

    container_type container_;
};

ContProxy<int, MyContainer<int, std::list<int>> p;

关于c++ - 部分模板模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10527775/

相关文章:

c++ - c程序内存布局,关于局部变量和全局变量的位置

c++ - 使用元编程计算非默认模板参数?

c++ - 模板参数包中的 6 个点是什么?

c++ - 在集合中的每个元素上调用打印的通用算法

c++ - 从委托(delegate)中的 C++ 模型获取数据

c++ - Mergecom 标签不按顺序 (MC_OUT_OF_ORDER_TAG) 问题

c++ - 通过自定义宏构造 boost::fusion::map(以及更多)

string - 如何保存呈现的模板而不是打印到 os.Stdout?

C++函数模板部分特化?

c++ - reinterpret_cast 模板类从非常量版本到常量版本