The examples I've seen of unrestricted unions always seem to use placement new when constructing. The Wikipedia article for C++11 features uses placement new in the constructor of a union.
https://en.wikipedia.org/wiki/C%2B%2B11#Unrestricted_unions
#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.
};
Is it necessary to use placement new here? For example, this piece of code compiles without warnings with gcc and valgrind shows no memory leaks when the union is used to hold a string:
struct HasUnresUnion
{
enum { Int, String } tag;
HasUnresUnion(int i)
: tag(Int),
as_int(i)
{}
HasUnresUnion(std::string str)
: tag(String),
as_str(std::move(str))
{}
~HasUnresUnion()
{
using std::string;
if (tag == String)
as_str.~string();
}
union
{
int as_int;
std::string as_str;
};
};
It doesn't seem like there's any ambiguity here so I don't see why the standard would outlaw this. Is this legal code? Is placement new necessary when the union is uninitialized (rather than being assigned to)? Is the constructor in the union required? I've definitely seen unrestricted unions without their own constructors but Wikipedia explicitly states it's required.
Introduction
The snippet you have shown is perfectly safe; you are legally allowed to initialize one non-static data-member when initializing your union-like class.
Example
The wikipedia article has an example where placement-new is used because they are writing to a member after the point in which it is possible to directly initialize a certain member.
union A {
A () { new (&s1) std::string ("hello world"); }
~A () { s1.~basic_string<char> (); }
int n1;
std::string s1;
};
The previous snippet is however semantically equivalent to the following, where we explicitly state that A::s1
shall be initialized when constructing A
.
union A {
A () : s1 ("hello world") { }
~A () { s1.~basic_string<char> (); }
int n1;
std::string s1;
};
Elaboration
In your snippet you have an anonymous union inside your class (which makes your class a union-like class), which means that unless you initialize one of the members of the union
during initialization of the class — you must use placement-new to initialize them at a later time.
What does the Standard say?
9.5/1 -- Unions -- [class.union]p1
In a union, at most one of the non-static data members can be active at any
time, that is, the value of at most one of the non-static data members can
be stored in a union at any time.
3.8/1 -- Object lifetime -- [basic.life]p1
[...]
The lifetime of an object of type T
begins when:
- storage with the proper alignment and size for type
T
is obtained, and
- if the object has non-trivial initialization, its initialization is complete.
The lifetime of an object of type T
ends when:
- if
T
is a class type with a non-trivial destructor (12.4), the destructor call starts, or
- the storage which the object occupies is reused or released.
No, placement new is not required here. The standard says that in case of unrestricted unions, the field constructors should be called explicitly, otherwise the fields will be uninitialized.
You can call the constructor using traditional way
U(): p() {}
and exotic way
U() { new(&p) Point(); }
The second variant may be useful if it is not possible to construct the field before calling the constructor of U
.
Don't also forget about placement destructors in this case.