Topic: Why are unnamed structs and unions disallowed?


Author: sgolodetz@dial.pipex.com ("Stuart Golodetz")
Date: Mon, 21 Apr 2003 13:58:54 +0000 (UTC)
Raw View
Just out of curiosity, why should the following code (which uses unnamed
structs and unions) be disallowed?

struct geom_Vector
{
    union
    {
        struct
        {
            double x, y, z;
        };
        double v[3];
    };
};

A recent post in comp.lang.c++ ("C<T>: simultaneously accessing elements
with [0] and .x", Franz Gans) reminded me that I'd written code like the
above before which (as it turns out: incorrectly) compiled in VC++ (6.0).
That being the case, I'm curious as to the reasoning behind disallowing the
above. Does anyone know please?

Thanks,

Stuart.


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Mon, 21 Apr 2003 17:32:11 +0000 (UTC)
Raw View
""Stuart Golodetz"" <sgolodetz@dial.pipex.com> wrote in message news:3ea3f7f3$0$11377$cc9e4d1f@news.dial.pipex.com...
> Just out of curiosity, why should the following code (which uses unnamed
> structs and unions) be disallowed?

Anonymous unions are allowed.   Anoymous structs aren't.   I guess people
were more adamanent about unions.


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Mon, 21 Apr 2003 17:51:26 +0000 (UTC)
Raw View
"Ron Natalie" <ron@sensor.com> wrote...
>
> ""Stuart Golodetz"" <sgolodetz@dial.pipex.com> wrote in message
news:3ea3f7f3$0$11377$cc9e4d1f@news.dial.pipex.com...
> > Just out of curiosity, why should the following code (which uses unnamed
> > structs and unions) be disallowed?
>
> Anonymous unions are allowed.   Anoymous structs aren't.   I guess people
> were more adamanent about unions.


Actually, anonymous structs are allowed, you just need to create
an object of that type immediately:

    int main() {
        struct {
            int a,b;
        } ab;
        ab.a = 5;
    }

Of course, one may say that it is not unnamed any more since
there is a name for the object... :-)

Victor
--
Please remove capital A's from my address when replying by mail


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ron@sensor.com ("Ron Natalie")
Date: Mon, 21 Apr 2003 18:25:28 +0000 (UTC)
Raw View
""Victor Bazarov"" <v.Abazarov@attAbi.com> wrote in message news:va8bn57bgp8e6e@corp.supernews.com...

> Actually, anonymous structs are allowed, you just need to create
> an object of that type immediately:

Sorry.  That's not an anonymous struct.   An anonymous union has
neither a class name or a value.
>
>     int main() {
>         struct {
>             int a,b;
>         } ab;
>         ab.a = 5;
>     }
>
> Of course, one may say that it is not unnamed any more since
> there is a name for the object... :-)

C++ requires either a class name or a variable name (or both) for
structs/classes.   It doesn't for unions that are contained inside
others.

    struct foo {
        int x;
        union {
            char uc;
            short us;
        {;
    };

is legal.  The term "anonymous union" is defined in 9.5/2 as a unnamed
object of unamed type.


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kevlin@curbralan.com (Kevlin Henney)
Date: Tue, 22 Apr 2003 19:34:06 +0000 (UTC)
Raw View
In article <3ea3f7f3$0$11377$cc9e4d1f@news.dial.pipex.com>, Stuart
Golodetz <sgolodetz@dial.pipex.com> writes
>
>A recent post in comp.lang.c++ ("C<T>: simultaneously accessing elements
>with [0] and .x", Franz Gans) reminded me that I'd written code like the
>above before which (as it turns out: incorrectly) compiled in VC++ (6.0).
>That being the case, I'm curious as to the reasoning behind disallowing the
>above. Does anyone know please?

I think the reason may be as simple as: because Bjarne included anon
unions but not anon structs! Anonymous unions have been in the language
a long time and resolve a point of syntactic tedium, that of having to
name the union instance every time you want to access any of its
members, even though the union member names are typically distinct from
anything else at the same level.

The case for anon structs is less compelling, but there is one. Of
course, there is the argument by orthogonality and regularity that would
suggest that if anonymous unions are permitted then anonymous structs
should also be permitted.

An anonymous struct, whether at local or namespace level, would have the
property that the addresses of successive elements are in ascending
order, ie

        struct
        {
                int a, b;
        };
        int c, d;

It would be possible to state with confidence that &b > &a is both well
defined and true, which is not the case for &d > &c.

Kevlin
____________________________________________________________

  Kevlin Henney                   phone:  +44 117 942 2990
  mailto:kevlin@curbralan.com     mobile: +44 7801 073 508
  http://www.curbralan.com        fax:    +44 870 052 2289
  Curbralan: Consultancy + Training + Development + Review
____________________________________________________________

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]