c++ - 静态映射类到 std::type_index 中的值“表达式必须具有常量值”
问题描述
我正在尝试在 C++ 中构建一个结构,它将类作为键并将整数作为值。我想要这种静态映射,然后我可以在我的程序中使用它来轻松访问与某个类关联的 int。
这是我正在尝试编写的代码:
#include <unordered_map>
#include <typeindex>
#include <typeinfo>
#include "TransformC.h"
// map class -> int
std::unordered_map<std::type_index, int> comp_cols;
// populate map
comp_cols[std::type_index(typeid(TransformC))] = 0;
...
我在 Visual Studio 2017 上,在std::type_index
.
我不明白那个错误到底是什么,我不知道是否还有其他方法可以实现我想要实现的目标(可能是一些模板恶作剧在编译时map<Class>
用正确的索引替换 a ?)
仅当上面发布的代码在文件范围内时(我在函数范围内没有错误),我才会收到此错误。我不明白为什么在一种情况下存在错误,而在另一种情况下没有。
请帮忙!
解决方案
推荐阅读
- java - 如何使用 Java 中的 Sugaronrest 通过 API 向 SuiteCRM 中的潜在客户添加电子邮件?
- python - 从 Amazon S3 读取仅具有特定名称的文件
- java - 如何在不使用 DateTimeFormatter 的情况下将 String 转换为 LocalDate
- r - 链接地图点击事件与情节突出功能
- python - 如何在nodejs中使用繁重的python脚本?
- azure-logic-apps - 创建 SQL 表后,逻辑应用程序跳过 SQL 新行触发器
- visual-studio - 如何在 Visual Studio 项目模板中将 XAML 设置为嵌入式资源(而不是页面)?
- python - 在不使用循环的情况下分段着色 matplotlib.pyplot 线图
- tsql - 在 MS Access 中对传递查询使用自联接
- arcmap - 在 Fusion 和 ArcMap 中显示植被比的问题