Initialization of an anonymous struct within a class

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Initialization of an anonymous struct within a class

Shaun Jackman
Hi,

I'm not sure whether an anonymous struct within a class is standard, or
a GCC extension that clang attempts to support. In any case, the
following code snippet gives this error:

anonymous-struct.cc:9:16: error: multiple initializations given for non-static member 'y'
        Foo() : x(0), y(0) { }
                      ^~~~
anonymous-struct.cc:9:10: note: previous initialization is here
        Foo() : x(0), y(0) { }
                ^
2 diagnostics generated.

Just for comparison, I've also given code that does not use an anonymous
struct and does compile cleanly.

Thanks,
Shaun

#define DOES_NOT_WORK 1
#if DOES_NOT_WORK
struct Foo
{
        struct {
                int x;
                int y;
        };
        Foo() : x(0), y(0) { }
};
#else
struct Foo
{
        struct S {
                int x;
                int y;
        } s;
        Foo() : s((S){ .x = 1, .y = 2}) { }
};
#endif

#include <iostream>

int main()
{
        Foo foo;
#if DOES_NOT_WORK
        std::cout << foo.x << ' ' << foo.y << '\n';
#else
        std::cout << foo.s.x << ' ' << foo.s.y << '\n';
#endif
        return 0;
}


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of an anonymous struct within a class

Douglas Gregor

On Aug 9, 2010, at 12:58 PM, Shaun Jackman wrote:

> Hi,
>
> I'm not sure whether an anonymous struct within a class is standard, or
> a GCC extension that clang attempts to support. In any case, the
> following code snippet gives this error:
>
> anonymous-struct.cc:9:16: error: multiple initializations given for non-static member 'y'
>        Foo() : x(0), y(0) { }
>                      ^~~~
> anonymous-struct.cc:9:10: note: previous initialization is here
>        Foo() : x(0), y(0) { }
>                ^
> 2 diagnostics generated.
>
> Just for comparison, I've also given code that does not use an anonymous
> struct and does compile cleanly.


It's definitely a Clang bug, and while I know I've seen this issue at least once before, it doesn't seem to be in our bug tracker. Could you file a bug on LLVM's Bugzilla?

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of an anonymous struct within a class

Shaun Jackman
On Mon, 2010-08-09 at 15:48 -0700, Douglas Gregor wrote:

> On Aug 9, 2010, at 12:58 PM, Shaun Jackman wrote:
>
> > Hi,
> >
> > I'm not sure whether an anonymous struct within a class is standard, or
> > a GCC extension that clang attempts to support. In any case, the
> > following code snippet gives this error:
> >
> > anonymous-struct.cc:9:16: error: multiple initializations given for non-static member 'y'
> >        Foo() : x(0), y(0) { }
> >                      ^~~~
> > anonymous-struct.cc:9:10: note: previous initialization is here
> >        Foo() : x(0), y(0) { }
> >                ^
> > 2 diagnostics generated.
> >
> > Just for comparison, I've also given code that does not use an anonymous
> > struct and does compile cleanly.
>
>
> It's definitely a Clang bug, and while I know I've seen this issue at least once before, it doesn't seem to be in our bug tracker. Could you file a bug on LLVM's Bugzilla?
>
> - Doug

Hi Doug,

Is an anonymous struct like this one standard?

Bug 7855 - Initialization of an anonymous struct within a class
http://llvm.org/bugs/show_bug.cgi?id=7855

Cheers,
Shaun

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Initialization of an anonymous struct within a class

Douglas Gregor

On Aug 9, 2010, at 4:16 PM, Shaun Jackman wrote:

> On Mon, 2010-08-09 at 15:48 -0700, Douglas Gregor wrote:
>> On Aug 9, 2010, at 12:58 PM, Shaun Jackman wrote:
>>
>>> Hi,
>>>
>>> I'm not sure whether an anonymous struct within a class is standard, or
>>> a GCC extension that clang attempts to support. In any case, the
>>> following code snippet gives this error:
>>>
>>> anonymous-struct.cc:9:16: error: multiple initializations given for non-static member 'y'
>>>       Foo() : x(0), y(0) { }
>>>                     ^~~~
>>> anonymous-struct.cc:9:10: note: previous initialization is here
>>>       Foo() : x(0), y(0) { }
>>>               ^
>>> 2 diagnostics generated.
>>>
>>> Just for comparison, I've also given code that does not use an anonymous
>>> struct and does compile cleanly.
>>
>>
>> It's definitely a Clang bug, and while I know I've seen this issue at least once before, it doesn't seem to be in our bug tracker. Could you file a bug on LLVM's Bugzilla?
>>
>> - Doug
>
> Hi Doug,
>
> Is an anonymous struct like this one standard?

No, anonymous structs are a GCC extension. We have reasonably good support for them in C, but not necessarily in C++.

> Bug 7855 - Initialization of an anonymous struct within a class
> http://llvm.org/bugs/show_bug.cgi?id=7855

Thank you!

        - Doug
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev