Topic: type definition in anonymous union


Author: "Al Grant" <tnarga@arm.REVERSE-NAME.com>
Date: Fri, 31 May 2002 07:32:46 CST
Raw View
Is this legal?  Should it be?

  struct E {
    union {
      struct { int x; } s;
    } v;
  };

One compiler faults a type definition (i.e. of the anonymous
struct) since it is in an anonymous union [9.5].

I would suggest that compiler B is correctly interpreting the
standard but that this is a defect in the standard.  There is
no reason to disallow definition of anonymous structs.

Furthermore, is it really necessary to disallow definition of
named types in anonymous unions in general, as long as the
types do not need fully qualified names for external linkage?
Why should this be illegal?

  struct E {
    union {
      typedef int Int;
      struct X { X *next; Int n; } list;
    } v;
  };


---
[ 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: Steve Clamage <clamage@eng.sun.com>
Date: Mon, 3 Jun 2002 10:29:06 CST
Raw View
On Fri, 31 May 2002, Al Grant wrote:

> Is this legal?  Should it be?

Yes, and yes.

>
>   struct E {
>     union {
>       struct { int x; } s;
>     } v;
>   };
>
> One compiler faults a type definition (i.e. of the anonymous
> struct) since it is in an anonymous union [9.5].

Your example does not use an anonymous struct. It uses a struct
that has no tag, but which is valid.

An anonymous struct, by analogy with the description of anonymous
union, would be a struct definition that declares neither a type
name nor an object:

struct E {
  union {
    struct { int x; }; // anonymous struct
  } v;
};


> I would suggest that compiler B is correctly interpreting the
> standard but that this is a defect in the standard.  There is
> no reason to disallow definition of anonymous structs.

Compiler B is wrong.

Anonymous structs are not allowed by the C++ standard, but I would
not describe the rule as a defect. A defect is an error,
inconsistency, or omission (such as a missing description). See
the FAQ for this newsgroup for more details.

Allowing anonymous structs would be an extension that someone might
reasonably propose for a future version of the standard, particularly
because some implementations currently have the extension. I don't
believe anonymous structs have been formally proposed.

--
Steve Clamage, stephen.clamage@sun.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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Gennaro Prota <gennaro_prota@yahoo.com>
Date: Mon, 3 Jun 2002 16:43:42 GMT
Raw View
Yes, I think Al was a little confused because a) his code snippets do
not have any anonymous union; b) there is no such a thing like an
"anonymous struct" in C++.

But one of his questions I wouldn't have been able to answer: why are
definitions of types in anonymous unions prohibited (9.5p2)? Guess: no
reason to complicate the language even further, since you can always
declare those types in the enclosing scope?


Genny.

---
[ 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                       ]