Anonymous union members problem
llewelly@xmission.com
llewelly@xmission.com
Mon Apr 19 14:53:00 GMT 2004
Mathieu Fluhr <mfluhr@nero.com> writes:
> Hello
>
> I am porting VC++ source code to Linux using g++ 3.3 and I am currently
> getting some errors with C++ code like this sample program:
>
>
> struct foo
> {
> struct
Anonymous structs are never described in the ISO C++
standard. Therefor they are an extension or an error. GCC supports
anonymous structs with -fms-extensions.
> {
> typedef int structType;
However, gcc does not support typedefs within anonymous structs, even
with -fms-extensions. I don't know why. (Probabably because
standard C++ does not allow typedefs within anonymous unions.) If
MS supports typedefs within anonymous unions, you might consider
submiting a feature request.
Better, email MS and ask them to stop encouraging people to use
extensions. :-)
> union
> {
> int test;
> };
> };
> };
>
> int main()
> {
> return 0;
> }
>
> When I try to compile this program, I get the following message:
>
> mathieu@c-l-175:~/tests/bitfields$ g++-3.3 -Wall bitfields.cpp
> bitfields.cpp:5: error: `typedef int foo::<anonymous
> struct>::structType'
> invalid; an anonymous union can only have non-static data members
>
> Is this invalid C++ code ?
Yes. Anonymous structs are mentioned nowhere in the
standard. (Structs, and classes in general are covered in
ch 9. Anonymous unions are in 9.5/2 .)
> If yes is there a way to make it valid
struct foo
{
typedef int structType;
union { int test; };
};
> and
> compilable under Linux ?
struct foo
{
typedef int structType;
//Need -fms-extensions for anonymous struct.
struct { union { int test; }; };
};
Now I have a question for you: What is an anonymous struct good for?
More information about the Gcc-help
mailing list