Topic: Does This Comply With The C++ Standard?
Author: "Niranjan Suri" <nsuri@ai.uwf.edu>
Date: 1999/03/10 Raw View
Hello,
I wrote some code using anonymous structs which works fine in Visual C++ but
fails to compile in other C++ compilers (SGI and EGCS). Can someone tell me
if the following code is standard C++ or if it is a Microsoft extension?
struct Test
{
union {
struct {
int a;
float f;
};
double d;
};
};
int main (void)
{
Test t;
t.a = 10;
return 0;
}
This code basically declares test to be a struct that contains either an int
and a float or a double. If this is not valid C++, is there another way to
express the same semantics (besides giving the inner struct that contains a
and f a name?)
Thanks!
Sincerely,
Niranjan
----------------------------------------------------------------------
Niranjan Suri nsuri@ai.uwf.edu
University of West Florida
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/03/10 Raw View
Niranjan Suri wrote in message <7c52ae$fqj$1@news.fsu.edu>...
>
>Hello,
Hi.
>I wrote some code using anonymous structs which works fine in Visual C++ but
>fails to compile in other C++ compilers (SGI and EGCS). Can someone tell me
>if the following code is standard C++ or if it is a Microsoft extension?
>
>struct Test
>{
> union {
> struct {
> int a;
> float f;
> };
> double d;
> };
>};
There is no such thing as an anonymous struct
in standard C++. Your code relies upon a
Microsoft-specific extension. You can see this
for yourself in the online help for Visual C++,
under the topic titles "Anonymous Structures",
where it says "Microsoft Specific" and "C++
does not allow anonymous structures." If you
compile with the -Za switch, (which means
"disable extensions"), the compiler will reject
your code complaining "illegal declaration of
anonymous 'struct' ".
[snip]
>This code basically declares test to be a struct that contains either an int
>and a float or a double. If this is not valid C++, is there another way to
>express the same semantics (besides giving the inner struct that contains a
>and f a name?)
I don't think so. If there was, I think it would
not have been created as a C extension.
--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/03/10 Raw View
"Niranjan Suri" <nsuri@ai.uwf.edu> writes:
> I wrote some code using anonymous structs which works fine in Visual C++ but
> fails to compile in other C++ compilers (SGI and EGCS). Can someone tell me
> if the following code is standard C++ or if it is a Microsoft
> extension?
This programm is ill-formed. While a anonymous union can be used to
define an anonymous object, an anonymous struct doesn't.
Regards,
Martin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/03/12 Raw View
Larry Brasfield wrote:
>
> There is no such thing as an anonymous struct
> in standard C++.
The following code compiles on egcs and VC++ (with -Za switch):
struct
{
int x;
} aX;
IMHO, this is something that can be called as anonymous struct. Did I
miss something?
--
Best regards,
Biju Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/12 Raw View
Biju Thomas wrote:
>
> Larry Brasfield wrote:
> >
> > There is no such thing as an anonymous struct
> > in standard C++.
>
> The following code compiles on egcs and VC++ (with -Za switch):
>
> struct
> {
> int x;
> } aX;
>
> IMHO, this is something that can be called as anonymous struct. Did I
> miss something?
Yes. The fact that egcs and VC++ neither define, nor even completely
conform with the standard, even when all of the apparantly relevant
compiler options are set. Even in a fully conforming mode, an
implementation is allowed to accept code that isn't strictly conforming.
The standard doesn't allow anonymous structs in strictly conforming
code.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/03/13 Raw View
> > The following code compiles on egcs and VC++ (with -Za switch):
> >
> > struct
> > {
> > int x;
> > } aX;
> >
> > IMHO, this is something that can be called as anonymous struct. Did I
> > miss something?
>
> Yes. The fact that egcs and VC++ neither define, nor even completely
> conform with the standard, even when all of the apparantly relevant
> compiler options are set. Even in a fully conforming mode, an
> implementation is allowed to accept code that isn't strictly conforming.
That's a nice diatribe, but it has nothing to do with the issue.
What he wrote was legal, and is *NOT* an anonymous struct.
It's just an unnamed type. An anonymous union is not only
an unnamed-object as well. It's members act as if they
were part of the enclosing scope directly. What you have
above is just an unnamed type, but it exists non-ambiguously
as aX in the scope it was defined in. Lots of C backwards
compatibility would break if it were not allowed.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/03/13 Raw View
In article <36E83E88.F728C8A7@ibm.net>, Biju Thomas <bijuthom@ibm.net>
writes
>The following code compiles on egcs and VC++ (with -Za switch):
>
> struct
> {
> int x;
> } aX;
>
>IMHO, this is something that can be called as anonymous struct. Did I
>miss something?
Yes.
1) the compiler will generate a name for internal usage.
2) you have to use either dot or arrow to access the members (compare
that with the requirements for an anonymous union)
>
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/03/13 Raw View
Biju Thomas wrote in message <36E83E88.F728C8A7@ibm.net>...
>Larry Brasfield wrote:
>>
>> There is no such thing as an anonymous struct
>> in standard C++.
>
>The following code compiles on egcs and VC++ (with -Za switch):
>
> struct
> {
> int x;
> } aX;
>
>IMHO, this is something that can be called as anonymous struct. Did I
>miss something?
I object to calling it an "anonymous struct" for these reasons:
(1). It is "anonymous" in a different sense than the word was
used in my original post mentioning "anonymous union".
As that term is used in the standard, it refers to a union
whose members can be named without a qualifier that
specifies the union. The union member names just flow
into the scope in which the union is defined. That is not
the case for the above code, so "anonymous" clearly
means something different here. The anonymity in the
standard's "anonymous union" refers to the namelessness
of the subobject. Yours refers to an absent typename.
(2). It is not anonymous. Its name is "aX".
--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.com )
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]