在地图中使用std :: type_index作为值
Use std::type_index as value in a map
我正在尝试创建一个 std::unordered_map
,其中 value 是 std::type_index
。以下片段有效:
std::unordered_map<std::type_index, int> workingMap;
workingMap[typeid(int)] = 1;
workingMap[typeid(char)] = 2;
,但这不会运行并引发错误:
std::unordered_map<int, std::type_index> failingMap;
failingMap[1] = typeid(int);
failingMap[2] = typeid(char);
cs2512:'std :: type_index :: type_index':没有适当的默认构造器可用。
我不完全理解此错误,这些示例中的构造函数有什么区别?是否可以制作 typeid(..)
是 value 而不是密钥的地图?
问题是 operator[]
,而不是地图的实际使用。问题是,如果键是未找到的,operator[]
将分配默认值并将可修改的引用返回到该值,而std::type_index
是不可能的。您可以使用emplace
,insert
,try_emplace
或任何其他不需要默认构造函数的修饰符。
我们当然可以为type_info
创建一个无效的包装器。
#include <typeindex>
#include <functional>
#include <unordered_map>
#include <cassert>
struct nullable_type_index
{
constexpr nullable_type_index() : ptr_(nullptr) {}
constexpr nullable_type_index(std::type_info const& ti) : ptr_(std::addressof(ti)) {}
constexpr operator bool() const
{
return bool(ptr_);
}
// I have avoided implicit conversion, but it could probably work
// without any surprises.
std::type_info const& get_type_info() const {
assert(ptr_);
return *ptr_;
}
constexpr bool operator==(nullable_type_index const& other) const {
return ptr_ && other.ptr_
? *ptr_ == *other.ptr_
: ptr_ == other.ptr_;
}
private:
std::type_info const* ptr_;
};
std::size_t hash_value(const nullable_type_index& nti)
{
return nti ? 0 : nti.get_type_info().hash_code();
}
bool operator==(nullable_type_index const& l, std::type_info const& r)
{
return l == nullable_type_index(r);
}
bool operator==(std::type_info const& l, nullable_type_index const& r)
{
return nullable_type_index(l) == r;
}
namespace std {
template<>
struct hash<nullable_type_index>
{
std::size_t operator()(nullable_type_index const& arg) const {
return hash_value(arg);
}
};
}
int main()
{
std::unordered_map<std::type_index, int> workingMap;
workingMap[typeid(int)] = 1;
workingMap[typeid(char)] = 2;
std::unordered_map<int, nullable_type_index> failingMap;
failingMap[1] = typeid(int);
failingMap[2] = typeid(char);
}
或当然,现在我们有std ::可选...
int main()
{
std::unordered_map<std::type_index, int> workingMap;
workingMap[typeid(int)] = 1;
workingMap[typeid(char)] = 2;
std::unordered_map<int, std::optional<std::type_index>> failingMap;
failingMap[1] = typeid(int);
failingMap[2] = typeid(char);
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- DrawIndexedInstanced 具有不同的 Index Count per Instance (Directx
- 控制到达非空函数clang(-Wreturn-type)的末尾
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- OpenGL VBO Indexing ( How to compute Index Array)
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 如何根据排序索引的向量对 std::index 集进行排序?
- 将内存分配返回值强制转换为 TYPE 数组