Variables whose types are anonymous structs, once again
Matt Austern
austern@apple.com
Thu Apr 8 22:11:00 GMT 2004
OK, I raised this issue once before. I know more now than I did then,
so I'm raising it again.
The mainline C++ compiler gives an error (a pedwarn, which is treated
as an error by default) if you do this:
enum { a } x;
The error message, and the comments in the source code, say that the
compiler is enforcing clause 3.5 paragraph 8: "A name with no linkage
(notably, the name of a class or enumeration declared in a local scope
(3.3.2)) shall not be used to declare an entity with linkage."
At a minimum, that comment is wrong. Whatever it is we're declaring x
with, it's not "a name with no linkage". Its type is a anonymous enum.
Tautology: since this type has no name, it can't have "a name with no
linkage".
Now the next question: given that 3.5 paragraph 8 doesn't apply, should
we accept or reject this code? There the question gets more
interesting.
It turns out that a lot of people have found what the standard says
about this to be pretty murky. I found three different issues on the
CWG issues list that related directly to this case, and maybe a half
dozen that were tangentially related. Here are the ones that seem most
directly relevant:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#132
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#278
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#389
Note that issue 132 has NAD status, and it was rejected as NAD
specifically because prohibiting code like what I listed above "would
have introduced an incompatibility with the C language" because this
example "represents an idiom that is used in C".
Issue 389, which has "review" status, has a different proposed
resolution: the proposed resolution would explicitly ban this code.
(For those who are unfamiliar with WG21 bureaucracy, "review" status
means that the core working group is leaning towards this solution,
that they have precise standardese that they're considering, but that
the committee hasn't yet voted to accept this wording.)
So putting all this together, what I get is that: (1) as it stands
right now the standard isn't as clear as it might be, but a close
reading shows that allows "enum {a} x". (2) The committee is leaning
towards saying that that's a mistake and that this construct should be
forbidden.
My feeling: we should do what the standard says, not what we think the
committee will do in a future version of the standard. Unless someone
can come up with an argument that my reading is wrong, I'm going to
submit a patch to allow this.
--Matt
More information about the Gcc
mailing list