The question is whether the following is undefined behaviour (and if not, what guarantees do I have):
using kv = std::pair<std::string, int>;
using ckv = std::pair<const std::string, int>;
kv x = {"239", 239};
const kv* p = &x;
std::cout << p->first << " " << p->second << "\n";
// Dangerous cast
const ckv* cp = static_cast<const ckv*>(static_cast<const void*>(p));
std::cout << cp->first << " " << cp->second << "\n";
I know that kv and ckv are two different types and hence pointers are incompatible, but perhaps I am wrong? The snippet above compiles on my machine and does the expected thing - is it just a "happy coincidence" due to two types having similar layout that hides undefined behaviour or is it something I can depend on reliably?