Most vexing parse

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.

Occurrence
The term "most vexing parse" was first used by Scott Meyers in his 2001 book Effective STL. While unusual in C, the phenomenon was quite common in C++ until the introduction of uniform initialization in C++11.

C-style casts
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  with initial value produced by converting   to an. However, C allows superfluous parentheses around function parameter declarations; in this case, the declaration of  is instead a function declaration equivalent to the following:

Unnamed temporary
A more elaborate example is:

The line

is ambiguous, since it could be interpreted either as
 * 1) a variable definition for variable time_keeper of class TimeKeeper, initialized with an anonymous instance of class Timer or
 * 2) a function declaration for a function time_keeper that returns an object of type TimeKeeper and has a single (unnamed) parameter, whose type is a (pointer to a) function taking no input and returning Timer objects.

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: $ clang++ time_keeper.cc timekeeper.cc:9:25: parentheses were disambiguated as a function declaration [-Wvexing-parse] TimeKeeper time_keeper(Timer);  timekeeper.cc:9:26: note: add a pair of parentheses to declare a variable TimeKeeper time_keeper(Timer); timekeeper.cc:10:21: member reference base type 'TimeKeeper (Timer (*))' is not a structure or union return time_keeper.get_time; 

Solutions
The required interpretation of these ambiguous declarations is rarely the intended one. Function types in C++ are usually hidden behind typedefs and typically have an explicit reference or pointer qualifier. To force the alternate interpretation, the typical technique is a different object creation or conversion syntax.

In the type conversion example, there are two alternate syntaxes available for casts: the "C-style cast"

or a named cast:

In the variable declaration example, the preferred method (since C++11) is uniform (brace) initialization. This also allows limited omission of the type name entirely: Prior to C++11, the common techniques to force the intended interpretation were use of an extra parenthesis or copy-initialization: In the latter syntax, the copy-initialization is likely to be optimized out by the compiler. Since C++17, this optimization is guaranteed.