template std :: map作为类成员

时间:2014-03-04 10:00:36

标签: c++ templates stdmap

我需要像这样的类成员:

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

错误消息:template is not allowed

有人可以帮我解决这个问题吗?

THX

3 个答案:

答案 0 :(得分:1)

您可以从地图声明中删除template<class>

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

答案 1 :(得分: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;
};

答案 2 :(得分:0)

如果您打算将std::map作为类模板参数std::map实际上需要四个模板参数,其中两个是默认的。

#include <map>

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

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

然后你可以输入它:

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

(可选std::stringint是您传递给func的模板参数。)

相关问题