For questions about the "most vexing parse", a particular syntactic ambiguity in C++ programs that leads to a counterintuitive interpretation of certain declarations. (The name was coined by Scott Meyers in "Effective STL".) It is often accompanied by poor diagnostics, confusing many programmers who encounter it.
The most vexing parse refers to a particular C++ syntactic ambiguity where a declaration statement that can be interpreted either as an object declaration with an initializer that is a temporary object or as a function declaration is resolved, by rule, to a function declaration.
As an example, the line
MyObject obj(4);
declares an object of type MyObject
named obj
passing in 4 as a parameter. However, the line
MyObject obj(OtherType());
does not declare obj
as an object constructed from a value initialized OtherType
temporary, but instead declares a function named obj
that takes a pointer to a function taking no arguments and returning an OtherType
. (Function parameters declared as having function type are automatically adjusted to the corresponding pointer to function type in the same way that function parameters declared with array types are adjusted to the corresponding pointer type.)
To change the declaration into an object type the following alternatives can be used.
MyObject obj1( (OtherType()) ); // extra parentheses
MyObject obj1 = OtherType(); // Requires an implicit conversion between types
// and requires MyObject to be copyable (or movable
// in C++11)
MyObject obj1 {OtherType()}; // C++11 syntax