The most vexing parse is a counterintuitive form of syntactic ambiguity resolution in the C++ programming language. In certain situations, the C++ grammar cannot distinguish between the creation of an object parameter and specification of a function's type. In those situations, the compiler is required to interpret the line as a function type specification.
The term "most vexing parse" was first used by Scott Meyers in his 2001 book Effective STL.[1] While unusual in C, the phenomenon was quite common in C++ until the introduction of uniform initialization in C++11.[2]
A simple example appears when a functional cast is intended to convert an expression for initializing a variable:
Line 2 above is ambiguous. One possible interpretation is to declare a variable i
with initial value produced by converting my_dbl
to an int
. However, C allows superfluous parentheses around function parameter declarations; in this case, the declaration of i
is instead a function declaration equivalent to the following:
A more elaborate example is:
struct TimeKeeper ;
int main
The line
is ambiguous, since it could be interpreted either as
The C++ standard requires the second interpretation, which is inconsistent with the subsequent line 10 above. For example, Clang++ warns that the most vexing parse has been applied on line 9 and errors on the subsequent line 10:[4] $ clang++ time_keeper.cc timekeeper.cc:9:25: parentheses were disambiguated as a function declaration [-Wvexing-parse] TimeKeeper time_keeper(Timer);