* Anonymous union members problem
@ 2004-04-19 13:09 Mathieu Fluhr
2004-04-19 13:15 ` Eljay Love-Jensen
2004-04-19 14:53 ` llewelly
0 siblings, 2 replies; 6+ messages in thread
From: Mathieu Fluhr @ 2004-04-19 13:09 UTC (permalink / raw)
To: gcc-help
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
{
typedef int structType;
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 ? If yes is there a way to make it valid and
compilable under Linux ?
Regards,
Mathieu
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Anonymous union members problem
2004-04-19 13:09 Anonymous union members problem Mathieu Fluhr
@ 2004-04-19 13:15 ` Eljay Love-Jensen
2004-04-19 13:55 ` Mathieu Fluhr
2004-04-19 14:53 ` llewelly
1 sibling, 1 reply; 6+ messages in thread
From: Eljay Love-Jensen @ 2004-04-19 13:15 UTC (permalink / raw)
To: Mathieu Fluhr, gcc-help
Hi Mathieu,
>Is this invalid C++ code ?
Yes, this is invalid C++ code.
>If yes is there a way to make it valid and compilable under Linux ?
Take the typedef out of the anonymous struct.
--or--
Name the anonymous struct so it is no longer anonymous.
I favor the second.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Anonymous union members problem
2004-04-19 13:15 ` Eljay Love-Jensen
@ 2004-04-19 13:55 ` Mathieu Fluhr
2004-04-19 14:28 ` Eljay Love-Jensen
2004-04-19 14:59 ` llewelly
0 siblings, 2 replies; 6+ messages in thread
From: Mathieu Fluhr @ 2004-04-19 13:55 UTC (permalink / raw)
To: Eljay Love-Jensen; +Cc: gcc-help
Hello Eljay
Thanks for answering so quiclkly. I still have some questions/remarks...
> >Is this invalid C++ code ?
>
> Yes, this is invalid C++ code.
>
Humm... do you have any reference about that ?
> >If yes is there a way to make it valid and compilable under Linux ?
>
> Take the typedef out of the anonymous struct.
> --or--
> Name the anonymous struct so it is no longer anonymous.
>
> I favor the second.
>
I already thought about these 2 possibilities... but none of them is
actually suitable: this anonymous struct is generated by a macro that
take a type as parameter (aim of the typedef) which is particular to
this structure.
So any other idea ?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Anonymous union members problem
2004-04-19 13:55 ` Mathieu Fluhr
@ 2004-04-19 14:28 ` Eljay Love-Jensen
2004-04-19 14:59 ` llewelly
1 sibling, 0 replies; 6+ messages in thread
From: Eljay Love-Jensen @ 2004-04-19 14:28 UTC (permalink / raw)
To: Mathieu Fluhr; +Cc: gcc-help
Hi Mathieu,
>Humm... do you have any reference about that ?
Try "g++ -pedantic -Wall -W foo.cpp".
Anonymous structures and classes are not C++. (Anonymous unions are C++.)
ISO 14882:1998 section 9.1
>I already thought about these 2 possibilities... but none of them is
actually suitable: this anonymous struct is generated by a macro that take
a type as parameter (aim of the typedef) which is particular to this
structure. So any other idea ?
Then your code is not C++, it's merely C++-ish.
You'll need to use a compiler that is compatible with your source code's
C++-ish variant. There may be a GCC flag that relaxes the rules and would
allow your code to compile with GCC 3.3. I don't know of such a flag, off
the top of my head.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Anonymous union members problem
2004-04-19 13:09 Anonymous union members problem Mathieu Fluhr
2004-04-19 13:15 ` Eljay Love-Jensen
@ 2004-04-19 14:53 ` llewelly
1 sibling, 0 replies; 6+ messages in thread
From: llewelly @ 2004-04-19 14:53 UTC (permalink / raw)
To: Mathieu Fluhr; +Cc: gcc-help
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?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Anonymous union members problem
2004-04-19 13:55 ` Mathieu Fluhr
2004-04-19 14:28 ` Eljay Love-Jensen
@ 2004-04-19 14:59 ` llewelly
1 sibling, 0 replies; 6+ messages in thread
From: llewelly @ 2004-04-19 14:59 UTC (permalink / raw)
To: Mathieu Fluhr; +Cc: Eljay Love-Jensen, gcc-help
Mathieu Fluhr <mfluhr@nero.com> writes:
[snip]
> I already thought about these 2 possibilities... but none of them is
> actually suitable: this anonymous struct is generated by a macro that
> take a type as parameter (aim of the typedef) which is particular to
> this structure.
>
> So any other idea ?
You must fix the macro.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-04-19 14:59 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-19 13:09 Anonymous union members problem Mathieu Fluhr
2004-04-19 13:15 ` Eljay Love-Jensen
2004-04-19 13:55 ` Mathieu Fluhr
2004-04-19 14:28 ` Eljay Love-Jensen
2004-04-19 14:59 ` llewelly
2004-04-19 14:53 ` llewelly
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).