This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: I'm sorry, but this is unacceptable (union members and ctors)
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: "michael.a" <wild dot eyed dot boy dot from dot freecloud at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 15 Jun 2007 16:50:09 -0700
- Subject: Re: I'm sorry, but this is unacceptable (union members and ctors)
- References: <11149318.post@talk.nabble.com>
On Fri, Jun 15, 2007 at 04:25:52PM -0700, michael.a wrote:
> I'm afraid I have a fairly major project which requires a Linux port. The
> problem is, development has been put off for a while because GCC lacks any
> means or work around which permits nesting ctors inside a union.
Rather, you're relying on a nonportable compiler extension. No matter,
you have a problem to fix. I guess there's some reason why you can't
use base classes and derived classes to store your data variants; perhaps
it's because you want to put them into an array?
One possible workaround is to use placement new. As I understand your
motivation, you want to be able to construct one of several types of
objects in the same fixed-size space that is large enough to hold any of
them. The union is replaced by a class whose data member is a char array
of sufficient size, and you can then construct sub-objects of whatever
type you want in the char array with placement new.