Is there a way to make the "most annoying parsing" be an error even on a class basis?

Is it possible (with any modification of class A) to do the following work? those. to make the most unpleasant parsing error?

class A { }; int main() { A a(); // can this be forced to be an error?? A b; // this should work } 
+4
source share
2 answers

No modification of class A will affect how the declaration of A a(); parsed A a(); . The parser determines that it is a function declaration before it even wants to look at definition A In fact, the definition of A should not even be visible for the analysis of this statement; Adequate ad is enough.

However, compilers usually have a warning for this, and you can probably turn this 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(); is not the syntax known as the most annoying parsing. It will be:

 A a(B()); 
+5
source

There is no way in the current language specification that could make this code an error. Typically, you just get a funny error message when you try to use the "object". However, some compilers 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?? ^~ 
+1
source

All Articles