https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70769

            Bug ID: 70769
           Summary: function definition wrongfully allowed inside comma
                    separated member declaration list
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: trivial
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fourmisain+gcc at gmail dot com
  Target Milestone: ---

This code should not compile while it does:

class C {
  int n, f(int) {
    return 42;
  }
};

To break it down (using C++14 standard draft n4140), the only
member-declaration (ยง9.2) which declares multiple members at once is of the
form
  attribute-specifier-seq(opt) decl-specifier-seq(opt)
member-declarator-list(opt);

Note that in the above code, the required semicolon at the end is missing, so
this is already in violation of the standard, but the code still compiles
without even a warning (using -std=c++14 -Wall -pedantic) if you add that
missing semicolon.

So continuing the argument, "int n, f(int) {return 42;}" would have to be a
member-declarator-list, so "f(int){return 42;}" would have to be a
member-declarator and since it does not contain a colon it is one of the two:
  declarator virt-specifier-seq(opt) pure-specifier(opt)
  declarator brace-or-equal-initializer(opt)
Since "{return 42;}" is not a valid brace-or-equal-initializer, "f(int){return
42;}" has to be a declarator.

Afaik, this is not true and it also conflicts with the following:
If it were a declarator, you would be allowed to use it inside a
init-declarator-list, therefore the following code would have to compile:

int n, f(int) {
  return 42;
}

But it (rightfully) does not:
"error: a function-definition is not allowed here before '{' token"

So this error message needs to be applied to the above class case.

--------
I was made aware that the above "hack" is often used on CodeFights to save
precious characters. They seems to wrap code into a class and compile it with
g++ 5.0.

Reply via email to