模板标准::映射为类成员
template std::map as class member
我需要这样的东西作为类成员:
std::map<std::string, std::map<std::string, template<class> T>> m_map;
错误消息:template is not allowed
有人可以帮助我解决这个问题吗?
感谢
您可以从映射的声明中删除template<class>
。
template<class T>
class A
{
std::map<std::string, std::map<std::string, T>> m_map;
};
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;
};
如果您打算将std::map
作为类模板参数,std::map
实际上需要四个模板参数,其中两个是默认的。
#include <map>
template <template <typename, typename, typename, typename> class T>
void func()
{
}
int main()
{
func<std::map>();
}
然后你可以键入def:
typedef T<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int>>> my_map;
((可选)std::string
和int
是传递给函数的模板参数。
相关文章:
- 将函数类成员映射到类本身内部
- 添加静态constexpr成员是否会更改结构/类的内存映射
- 整数键映射到头文件中的成员函数指针
- C++预处理会生成变量成员、资源库和映射
- 迭代嵌套映射与范围为循环:没有名为"first"的成员
- C++:在子类中扩展静态 STL 容器/映射成员?
- 用于对项目进行分组并将单个项目映射到其他组成员的数据结构
- C++-将对象(如字符串)映射到表中成员函数的正确方法
- 重构MFC消息映射以包括完全限定的成员函数指针
- gdb映射显示不正确的成员变量
- c++ 嵌套结构映射找不到成员
- 尝试通过指针映射访问类成员
- 映射类的数据成员
- std::映射键作为模板化结构与枚举成员
- 为什么擦除C++映射类成员中的值?
- 如何创建<id,成员函数>的映射?
- 向量映射作为实例成员的C 地图
- 如何使用成员函数映射参数包?(非恢复性,折叠前表达式)
- 删除类成员映射时,映射析构函数stl_map.h崩溃
- 在类定义中初始化静态常量成员映射<>