4

Is it possible (with any modification of class A) to have the following work? i.e., make the most vexing parse an error?

class A {
};

int main() {
    A a(); // can this be forced to be an error??
    A b;   // this should work
}
EHuhtala
  • 587
  • 3
  • 8
  • 2
    Unlikely. Remember that this is parsed as a function declaration, so you would need a modification to the type `A` that prevents `A` from being a return type of a function (even if no such function is ever even defined) while still being instantiable. If this is a real problem for you, I'd try to find a static analysis tool that will catch possible instances of this. – us2012 Sep 25 '13 at 15:42
  • 1
    Why? It's not an error. It's your expectations that are erred. – Luchian Grigore Sep 25 '13 at 15:44
  • 3
    This is **not** the [most vexing parse](http://en.wikipedia.org/wiki). It is simply a function declaration. – Pete Becker Sep 25 '13 at 15:52
  • If `A a()` was an error, how would you declare a function that takes no arguments and returns an object of type `A`? – Pete Becker Sep 25 '13 at 15:55
  • I could envision a checker in clang to disable local function declarations and complains there. Really, how many people do use local function declarations in real code? – David Rodríguez - dribeas Sep 25 '13 at 15:57
  • 1
    Oops, the link that I intended was [most vexing parse](http://en.wikipedia.org/wiki/Most_vexing_parse). – Pete Becker Sep 25 '13 at 21:00

2 Answers2

5

No modification of the class A will have any effect on how a declaration A a(); is parsed. The parser determines that this is a function declaration before it even bothers to look at the definition of A. In fact the definition of A doesn't even need to be visible to parse this statement; A forward declaration is sufficient.

However compilers generally have a warning for this and you can probably turn that into an error. For example with clang you can use the flag -Werror=vexing-parse.

struct A;

A a(); // no error

int main() {
    A a(); // error
}

clang++ -std=c++11 -Weverything -Werror=vexing-parse main.cpp

main.cpp:6:8: error: empty parentheses interpreted as a function declaration [-Werror,-Wvexing-parse]
    A a();
       ^~
main.cpp:6:8: note: replace parentheses with an initializer to declare a variable
    A a();
       ^~
       {}
1 error generated.

Although technically speaking A a(); isn't the syntax known as the most vexing parse. That would be:

A a(B());
bames53
  • 86,085
  • 15
  • 179
  • 244
1

There is no way in the current language specification which could make this code an error. Normally, you just get a funny error message when you try to use the "object". However, some compilers do warn about the situation (e.g. clang):

clang++ -W -Wall  -Werror -c -o vexing.o vexing.cpp
vexing.cpp:5:8: error: empty parentheses interpreted as a function declaration [-Werror,-Wvexing-parse]
   A a(); // can this be forced to be an error??
      ^~
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380