#include <iostream>
struct
{
int x, y;
struct { int a, b; };
} p;
int main()
{
p.a = 10;
return 0;
}
#include <stdio.h>
struct
{
int x, y;
struct { int a, b; };
} p;
int main(void)
{
p.a = 10;
return 0;
}
prog.c:6:22: error: ISO C99 doesn't support unnamed structs/unions [-Werror=pedantic]
struct { int a, b; };
^
I run the following C++ code at www.idepne.com using the C++14 mode of the compiler
#include <iostream>
struct
{
int x, y;
struct { int a, b; };
} p;
int main()
{
p.a = 10;
return 0;
}and it compiles successfully. However when I run a similar code using the C99strict mode
#include <stdio.h>
struct
{
int x, y;
struct { int a, b; };
} p;
int main(void)
{
p.a = 10;
return 0;
}then I got the following diagnostic message
prog.c:6:22: error: ISO C99 doesn't support unnamed structs/unions [-Werror=pedantic]
struct { int a, b; };
^Thus I wonder did I miss the day when the C++ Standard adopted anonymous structures in C++ or is it a own language extension of the compiler?
And did C99 have anonymous structures or were they adopted only in the C 2011?
It's an extension. C++ bans this in [class.mem]/2: "[...] each such member-declaration shall either declare at least one
member name of the class or declare at least one unnamed bit-field."Anonymous unions are permitted in C++, since by [class.union.anon]/1,struct A {union { int x, y; };};... declares x and y to be member names of A.
A storage class is not allowed in a declaration of an anonymous union in a class scope.
A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class X
has a set of variant members. If X is a union, a non-static data member of X that is not an anonymous union
is a variant member of X. In addition, a non-static data member of an anonymous union that is a member of X
is also a variant member of X.
On Tuesday, November 29, 2016 at 9:06:22 PM UTC-2, Richard Smith wrote:It's an extension. C++ bans this in [class.mem]/2: "[...] each such member-declaration shall either declare at least onemember name of the class or declare at least one unnamed bit-field."Anonymous unions are permitted in C++, since by [class.union.anon]/1,struct A {union { int x, y; };};... declares x and y to be member names of A.I fail to see in [class.union.anon] any reference to the fact that an anonymous union can be a data-member of a class,
or that a non-static data-member of an anonymous union is a data-member of the class containing this anonymous union,
other than a few tangential references on this respect, in paragraphs 2 and 4,as follows:§2:A storage class is not allowed in a declaration of an anonymous union in a class scope.§4 (emphasis is mine):A union-like class is a union or a class that has an anonymous union as a direct member. A union-like class X
has a set of variant members. If X is a union, a non-static data member of X that is not an anonymous union
is a variant member of X. In addition, a non-static data member of an anonymous union that is a member of X
is also a variant member of X.Note also that the definition of an anonymous union in [class.union.anon]/1 is not in the C++ grammar, and more, thedefinition of a member-declaration in [class.mem] seems to exclude an anonymous union from being contemplated by this definition.
On 30 November 2016 at 12:00, Belloc <jabe...@gmail.com> wrote:
On Tuesday, November 29, 2016 at 9:06:22 PM UTC-2, Richard Smith wrote:It's an extension. C++ bans this in [class.mem]/2: "[...] each such member-declaration shall either declare at least onemember name of the class or declare at least one unnamed bit-field."