Could someone explain this C++ union example?
问题 I found this code on cppreference.com. It's the strangest C++ I've seen, and I have a few questions about it: union S { std::string str; std::vector<int> vec; ~S() {} }; int main() { S s = { "Hello, world" }; // at this point, reading from s.vec is undefined behavior std::cout << "s.str = " << s.str << '\n'; s.str.~basic_string<char>(); new (&s.vec) std::vector<int>; // now, s.vec is the active member of the union s.vec.push_back(10); std::cout << s.vec.size() << '\n'; s.vec.~vector<int>(); }