This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/59729] New: C++11 allows type definitions in conditions


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

            Bug ID: 59729
           Summary: C++11 allows type definitions in conditions
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: harald at gigawatt dot nl

This came up on stackoverflow, not real code. It may be either a problem in the
standard, a problem in GCC, or even a problem in my reading.

The restriction on type definitions in conditions seems to have been lifted in
C++11. As such, the following program should be accepted:

int main() {
  if (struct S { } *p = 0) { }
}

However, GCC (just like clang) rejects it:

test.cc: In function âint main()â:
test.cc:2:16: error: types may not be defined in conditions
   if (struct S { } *p = 0) { }
                ^

Before C++11, this was an undeniably correct error message. [stmt.select]p2
explicitly stated "The type-specifier-seq shall not contain typedef and shall
not declare a new class or enumeration."

At some point, this rule got moved to type-specifier-seq, so the above was
removed, and [dcl.type]p3 now states: "A type-specifier-seq shall not define a
class or enumeration unless it appears in the type-id of an alias-declaration
(7.1.3) that is not the declaration of a template-declaration."

The condition rule then got changed to have a decl-specifier-seq instead of a
type-specifier-seq, per
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#948. There is
nothing to disallow class definitions in a decl-specifier-seq. However, the
issue doesn't mention lifting those restrictions at all, it is only about
allowing constexpr, so the fact that class definitions now seem to be allowed
may be accidental.

Either way, what GCC now implements doesn't appear to match what the standard
says.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]