我有一個需要使用某種地圖的類。默認情況下,我想使用std::map
,但我也想讓用戶能夠使用不同的東西(例如std::unordered_map
或者甚至可能是用戶創建的)。帶默認參數的C++模板模板參數
所以我有一些代碼,看起來像
#include <map>
template<class Key, template<class, class> class Map = std::map>
class MyClass {
};
int main() {
MyClass<int> mc;
}
但隨後,G ++抱怨
test.cpp:3:61: error: template template argument has different template parameters than its corresponding template template parameter
template<class Key, template<class, class> class Map = std::map>
^
test.cpp:8:14: note: while checking a default template argument used here
MyClass<int> mc;
~~~~~~~~~~~^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/map:781:1: note: too many template parameters in template template argument
template <class _Key, class _Tp, class _Compare = less<_Key>,
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.cpp:3:21: note: previous template template parameter is here
template<class Key, template<class, class> class Map = std::map>
^~~~~~~~~~~~~~~~~~~~~~
1 error generated.
所以它看起來像g++
是不滿,認爲std::map
有默認參數。
有沒有辦法讓Map
成爲可以接受至少兩個模板參數的模板?
如果可以的話,我寧願堅持使用C++ 98,但我願意接受C++ 11。
如果你打開C++ 11,你可以說'模板類地圖=的std :: map',但真正的解決辦法是參數化的類型,而不是一個模板。 –
2014-12-06 15:27:04
@KerrekSB我原本是這樣做的,但我需要地圖和地圖。我也有這些默認的模板參數,但我認爲如果用戶可以指定Map,並且他們沒有指定Map 或Map ,那將自動確定。有沒有更好的方式避免模板模板參數? –
math4tots
2014-12-06 15:45:26
* [C++模板函數默認值]的可能重複(http://stackoverflow.com/questions/3301362/c-template-function-default-value)*。 – 2015-06-23 22:08:06