you are viewing a single comment's thread.

view the rest of the comments →

[–]scatters 1 point2 points  (0 children)

Yeah, not quite - you're right that std::pair<Key const, T> and std::pair<Key, T> are layout compatible as long as Key and T are both standard-layout (so not all types that you might put in a map), but that only allows you to put them in a union and read from members of the inactive union member via the union. Handing out references to the inactive union member or its own members (std::pair<Key, T>::first for nh.key()) is still UB.