简体   繁体   English

STL容器上的模板模板参数

[英]template template parameters on STL container

I would like to implement a wrapper class that uses different key-value containers such as map, unordered_map. 我想实现一个包装器类,该包装器类使用不同的键值容器,例如map,unordered_map。

I hope user can use the code like this way: 我希望用户可以这样使用代码:

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

I use "template template paramteres" to achieve this, but the template parameters of map and unordered_map is different .. 我使用“模板模板参数”来实现这一点,但是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!!!

is there any ways to make a class that can pass map or unordered_map as template parameter?? 有什么方法可以使可以通过map或unordered_map作为模板参数的类? thanks! 谢谢!

您可能可以使用c ++ 11和可变参数模板执行所需的操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM