c++ - STL 容器上的模板模板参数

标签 c++ templates

我想实现一个包装类,它使用不同的键值容器,如 map、unordered_map。

希望用户可以这样使用代码:

MyWrapper<std::map> w1;
MyWrapper<std::tr1::unordered_map> w2;

我使用“template template paramteres”来实现这个, 但是 map 和 unordered_map 的模板参数不同..

// but this Wrapper is for std::map only....
template< template<typename,typename,typename,typename> class CONTAINER>
class MyWrapper
{
     CONTAINER<string, string,
            std::less<string>,
            std::allocator<std::pair<const string, string> > > c_;
};

MyWrapper<std::map> w1;
MyWrapper<std::tr1::unordered_map> w1; // not compiled!!!

有什么方法可以创建一个可以将 map 或 unordered_map 作为模板参数传递的类? 谢谢!

最佳答案

你可以使用 c++11 和可变参数模板来做你想做的事:

template< template<typename, typename...> class CONTAINER>

关于c++ - STL 容器上的模板模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13775557/

相关文章:

c++ - 在 It 是迭代器的 template<class It> 函数中,我可以使 It::value_type 同时适用于 vector::iterators 和 array::iterators 吗?

C++17 lambda 捕获 *this

c++ - 如何将整数字符串转换为二维整数 vector ?

.net - 在编码包装通过传递接口(interface)句柄工作的 C++ 接口(interface)(全部抽象)时 Intptr 是否足够?

c++ - 创建模板化结构的 std::list (C++)

c++ - C++ 中模板、仿函数、回调函数的练习题?

使用 std::string::c_str() 时的 C++11 类型转换 heisenbug

c++ - 水粒子模型

C++模板输入验证

c++ - 为什么 GCC 认为模板参数是 int 而它是完全不同的类型?