I would like to select a union member initialized in the constructor based on the argument. Below is an example that works:
struct A { union { int i; float f; }; A(double d, bool isint) { if (isint) new(&i) int(d); else new(&f) float(d); } };
While I use int and float , the goal is to work with other more complex types (but still valid in a C ++ 14 union), hence using place-new (rather than assignment).
The problem is that this constructor cannot be constexpr , because new-placement is not allowed in constexpr methods. Is there any way around this (other than creating part of the isint argument of the formal type system)? Some type of conditional list of initiators will work, but I do not know how to do this.
c ++ unions c ++ 14 constexpr
cshelton
source share