c++ c++11 constructor rtti typeinfo

c++ - Use std:: type_index como valor en un mapa



c++11 constructor (2)

Estoy intentando crear un std::unordered_map donde el valor es un std::type_index . El siguiente fragmento de código funciona:

std::unordered_map<std::type_index, int> workingMap; workingMap[typeid(int)] = 1; workingMap[typeid(char)] = 2;

Pero este no se ejecuta y arroja un error:

std::unordered_map<int, std::type_index> failingMap; failingMap[1] = typeid(int); failingMap[2] = typeid(char);

CS2512: ''std :: type_index :: type_index'': no ​​hay un constructor predeterminado apropiado disponible.

No entiendo completamente este error, ¿cuál es la diferencia entre los constructores en estos ejemplos? ¿Es posible hacer un mapa donde typeid(..) es el valor en lugar de la clave?


El problema es el operator[] , no el uso real del mapa. El problema es que si no se encuentra la clave, el operator[] asignará un valor predeterminado y devolverá una referencia modificable a ese valor, lo cual es imposible con std::type_index . Puede usar emplace , insert , try_emplace o cualquier otro modificador que no requiera un constructor predeterminado.


Por supuesto, siempre podríamos crear un contenedor anulable para 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); }

O por supuesto, ahora tenemos std :: opcional ...

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); }