#include <iostream>
#include <string>
using namespace std;

union U {
	string s;
	int i;
	U (string s) : s(s) { cout << "s is active"<<endl;}
	U (int i) : i(i) { cout << "i is active"<<endl;}
	U() : s() { cout << "s is active by default" <<endl; }
	~U() { cout << "delete... but what ?"<<endl; }
};

int main() {
	U u("hello"); 
	u.s += ", world";  
	cout << u.s <<endl;
	
	u.s.~string(); // properly end the life of s
	u.i=0;	// this is now the active member   
	           // no need to end life of an int, as it has a trivial destructor 
	new (&u.s) string("goodbye");  // placement new  
	cout << u.s <<endl;    
	u.s.~string(); // because our destructor doesn't know what's active
	
	return 0;
}