public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* assignment operators for union member classes
@ 2008-02-19 14:31 Mike Welsen
  2008-02-19 15:23 ` John Love-Jensen
  2008-03-06 22:57 ` Lawrence Crowl
  0 siblings, 2 replies; 3+ messages in thread
From: Mike Welsen @ 2008-02-19 14:31 UTC (permalink / raw)
  To: GCC -Help



Hi,

Does anyone know why asignment operators are not allowed for union class members ?
Other compilers accept this code, but I can't compile the following code with GCC.

( error: member 'A C::::::aa' with copy assignment operator not allowed in union)

class A{
public:
int a;
A& operator=(const A&){return (*this);}
int AFunc(void){}
};


class B{
public:
int b;
};


class C{
public:
C(){}
union
{
struct {A aa;}; // builds with warning if level4is set
B bb;
};
};

Thanks

_________________________________________________________________
Free games, great prizes - get gaming at Gamesbox. 
http://www.searchgamesbox.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: assignment operators for union member classes
  2008-02-19 14:31 assignment operators for union member classes Mike Welsen
@ 2008-02-19 15:23 ` John Love-Jensen
  2008-03-06 22:57 ` Lawrence Crowl
  1 sibling, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2008-02-19 15:23 UTC (permalink / raw)
  To: Mike Welsen, GCC-help

Hi Mike,

> Does anyone know why asignment operators are not allowed for union class
> members ?

Yes, I know why.

Because the assignment operator won't be executed.

(Besides that, a non-POD / non-aggregate object violates the constraints of
what can be in a union.)

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: assignment operators for union member classes
  2008-02-19 14:31 assignment operators for union member classes Mike Welsen
  2008-02-19 15:23 ` John Love-Jensen
@ 2008-03-06 22:57 ` Lawrence Crowl
  1 sibling, 0 replies; 3+ messages in thread
From: Lawrence Crowl @ 2008-03-06 22:57 UTC (permalink / raw)
  To: Mike Welsen; +Cc: GCC -Help

On 2/19/08, Mike Welsen <mikewelsen@hotmail.com> wrote:
> Does anyone know why asignment operators are not allowed for
> union class members?

The compiler needs to synthesize an assignment operator for the
union itself, but since the union's active field is not known, the
compiler cannot know which field to copy when copying the union.
So the compiler cannot do any better than copying all fields
simultaneously, which means using something like memcpy.  And so,
the C++98 standard makes using type with non-trivial assignment
operators illegal as union members.

> Other compilers accept this code, but I can't compile the following
> code with GCC.

Those other compilers are in error.

> (error: member 'A C::::::aa' with copy assignment operator
> not allowed in union)
>
> class A {
>   public:
>    int a;
>    A& operator=(const A&){return (*this);}
>    int AFunc(void){}
> };
>
> class B{
>   public:
>    int b;
> };
>
> class C {
>   public:
>    C(){}
>    union {
>      struct {A aa;}; // builds with warning if level4 is set
>      B bb;
>    };
> };

The C++0x standard will add a new feature that allows you to have
non-trivial assignment operators for union fields.  The catch is
that you will have to explictly code the surrounding assignment
operator, the compiler cannot figure it out.

-- 
Lawrence Crowl

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-03-06 22:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-19 14:31 assignment operators for union member classes Mike Welsen
2008-02-19 15:23 ` John Love-Jensen
2008-03-06 22:57 ` Lawrence Crowl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).