2012-12-08 45 views
1

我想实现一个使用不同键值容器(例如map,unordered_map)的包装类。STL容器上的模板模板参数

我希望用户可以使用这样的代码方式:

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

我用“模板模板paramteres”实现这一点, 但地图和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作为模板参数吗? 谢谢!

+1

只是有用户通过您的全部类型... – Xeo

回答

-1

您可以使用C++ 11和可变参数模板做你想做什么:

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

注意,这是没什么用的,因为模板有不同的参数,所以你需要区分它们。 – Xeo

+0

如果你唯一的担心是关键和价值,它似乎工作得很好。 – EHuhtala