2014-03-04 67 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通过模板参数。)