我想实现一个包装类,它使用不同的键值容器,如 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/