Hard
Placement new twice into the same buffer
What does this program print, or is it undefined behavior?
main.cpp
#include <iostream>
#include <new>
struct Widget {
const int id;
Widget(int i) : id(i) {}
};
int main() {
alignas(Widget) unsigned char buffer[sizeof(Widget)];
Widget* w1 = new (buffer) Widget(1);
Widget* w2 = new (buffer) Widget(2); // reuses the storage; ends w1's lifetime
std::cout << w1->id << std::endl; // accessing through the OLD pointer
}49 of 117 in C++