On Tue, Nov 11, 2008 at 6:53 PM, Mark Tall <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I've come across an oddity in C++, involving anonymous unions and
> const variables. Neither of the two classes below will compile using
> gcc 4.3.0.  Is this a bug in gcc or the C++ standard itself ?

No...

> class my_class_1
>  {
>  union
>    {
>    const int x;
>    const int y;
>    };
>
>  my_class_1() : x(0), y(0) {}
>  };
>
>
> class my_class_2
>  {
>  union
>    {
>    const int x;
>    const int y;
>    };
>
>  my_class_2() : x(0) {}
>  };

It's a bug in the code.

In a union only one field can be active at one time, hence
initializing more than one makes no sense ("If a ctor-initializer
specifies more than one mem-initializer for the same member, for the
same base class or for multiple members of the same union (including
members of anonymous unions), the ctor-initializer is ill-formed.")

However, const items need to be initialized, hence potting two in a
union makes no sense.

(The standard doesn't need an explicit rule to say that, as it's
implied by other rules.)

-- James

Reply via email to