I have the following unordered_map
s:
struct a{
std::string b;
};
int main()
{
std::unordered_map<std::string, std::string> source;
source["1"] = "Test";
source["2"] = "Test2";
std::unordered_map<std::string, a> dest = *reinterpret_cast<std::unordered_map<std::string, a>*>(&source);
std::cout << dest["1"].b << std::endl;
std::cout << dest["2"].b << std::endl;
}
Using a reinterpret_cast
I convert source
into dest
. This works since struct a
only consists of a std::string
.
My question: Is this actually good pratice? GCC yields the following warning:
dereferencing type-punned pointer will break strict-aliasing rules
Can I safely ignore this? Or is there any potential drawback of just casting the raw bytes of STL container?