2014-03-04 48 views
-1

我需要這樣的東西這是類成員:模板的std ::映射爲類成員

std::map<std::string, std::map<std::string, template<class> T>> m_map; 

錯誤消息:template is not allowed

有人可以幫我解決這個問題?

THX

+1

你能描述你想要什麼意思?它沒有任何意義,因爲它是。 –

+0

您是否僅僅想給'T'作爲第二個內部模板參數? –

+0

是的,這是什麼'模板'有:) –

回答

0

std::map<>預計(混凝土)類型參數,但是template<class> T不是一個類型,它遵循std::map<std::string, template<class> T>>不是一個類型。

不幸的是,「這樣的事情」並不是一個好的規範。

如果你真的是「地圖字符串(字符串的地圖,T)」,那麼下面的是一個正確的,可重複使用的解決方案:

// Declare a template type "map of string to (map of string to T)" 
template <typename T> 
using foobar = std::map<std::string, std::map<std::string, T>>; 

.... 

foobar<int> frob; 

http://ideone.com/YZ6FRa

作爲會員,一次性用品,這是可能的,太:

template <typename T> 
class Foobar { 
    std::map<std::string, std::map<std::string, T>> m_map; 
}; 
1

您可以從地圖上的聲明中刪除template<class>

template<class T> 
class A 
{ 
    std::map<std::string, std::map<std::string, T>> m_map; 
}; 
0

如果您打算有std::map類模板參數std::map實際需要四個模板參數,其中兩個是默認。

#include <map> 

template <template <typename, typename, typename, typename> class T> 
void func() 
{ 
} 

int main() 
{ 
    func<std::map>(); 
} 

然後你可以的typedef它:

typedef T<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int>>> my_map; 

(可選std::stringint你一起FUNC通過模板參數。)