In c++11, Union supports non-POD member. I want to initialize a non-POD member in the constructor.
On wikipedia c++11 page, it uses a placement 'new' to initialize a non-POD member.
#include <new> // Required for placement 'new'.
struct Point {
Point() {}
Point(int x, int y): x_(x), y_(y) {}
int x_, y_;
};
union U {
int z;
double w;
Point p; // Illegal in C++03; legal in C++11.
U() {new(&p) Point();} // Due to the Point member, a constructor definition is now required.
};
I am wondering is there any difference if I use ctor-initializer-list instead of placement 'new'?
U() : p() {}