On 23/09/2010 10:14 PM, bearophile wrote:
Justin Johansson:

One of the problems with C++ is that it is not possible
to create unions with non-primitive members (e.g. structs)
that have constructors.

Do you mean something like this?

struct S1 {
     int y;
     this(int x) { y = x; }
}

struct S2 {
     string t;
     this(string s) { t = s; }
}

union U {
     S1 s1;
     S2 s2;
}

static U u2 = { s2:S2("hello") };

void main() {
     U u = U(S1(10));
     assert(u.s1.y == 10);
     u.s2 = S2("hello");
     assert(u.s2.t == "hello");
     // U u3 = U(S2("hello")); // not possible
}

Yes, but not yes; something like that.  You are obviously
one step ahead of me so perhaps I should give up or else
post the exact problem in C++.  Still, it looks likes
from what you have shown that D has some better union
construction syntax than C++.

I hope others can throw in their 2 cents.

Bye,
Justin

Reply via email to