[Bug c++/15049] New: global variables with anonymous types are legal

austern at apple dot com gcc-bugzilla@gcc.gnu.org
Wed Apr 21 18:08:00 GMT 2004


The compiler recently changed to forbid the following construct, where 'x' is a global variable:
  enum { a = 77 } x;
(Or more specifically, the compiler now issues a pedwarn for this construct.  But since pedwarns are 
errors by default, it's fair to say that the compiler now forbids it.)

The comments in the code justify this by pointing to clause 3.5, paragraph 8: "A name with no linkage 
(notably, the name of a class or enumeration declared in a local scope) shall not be used to declare an 
entity with linkage".  

At the very least, this comment is wrong and needs to be clarified.  Yes, a global variable has linkage.  
But we are not declaring 'x' with "a name with no linkage".  x's type has no name, so it can't very well 
have "a name with no linkage".

I've gone through the C++ standards committee's Core Working Group issues list looking for 
clarification.  I've found a number of issues dealing with this.  The most directly relevant are:
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#132 (in NAD status)
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#389 (in review status)

Issue 132 was a request that the standard be changed to forbid declaring global variables with 
anonymous types.  The committee refused to do that, and the rationale was specifically that the 
standard currently allows it and that using anonymous enums to declare variable is a common C idiom 
that the committee did not want to break.

Issue 389 is a wholesale clarification of linkage rules as they apply to anonymous types.  It is in "review" 
status, meaning that the core working group is leaning toward this solution and is looking at proposed 
precise wording.  If the wording in issue 389 is accepted, then "enum {a} x" will be forbidden.

So the current status, I think, is pretty clear: in the current C++ standard (C++03), this construct is 
legal.  It will probably be made illegal in a future version of the standard.  ("C++0x")

This means that the compiler should not reject this construct.  This is a rejects-legal.  (And I note that 
this breaks some important programs, like one of the SPEC2004 programs and the Mac version of QT.)

We should probably issue a warning for this construct, since it's likely to be made illegal someday, but 
we shouldn't issue an error.

-- 
           Summary: global variables with anonymous types are legal
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: austern at apple dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: all
  GCC host triplet: all
GCC target triplet: all


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15049



More information about the Gcc-bugs mailing list