2012-12-08 82 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