Consider this :
float foo( int () )
This declares a function foo
( accepting a function returning int
) returning float
.
Now read
Y y(X());
as y
as function (accepting a function returning X
) returning Y
The problem arises due to C++ most vexing parse
Can be solved with :
Y y{ X() }; // requires C++11
or
Y y( ( X() ) );
// ^ ^ notice parenthesis
Update based on edit:
A quote from the standard :
§ 8.2 Ambiguity resolution [dcl.ambig.res]
1 - The ambiguity arising from the similarity between a function-style cast and a declaration mentioned in 6.8 can also occur in the context of a declaration. In that context, the choice is between a function declaration with a redundant set of parentheses around a parameter name and an object declaration with a function-style cast as the initializer. Just as for the ambiguities mentioned in 6.8, the resolution is to consider any construct that could possibly be a declaration a declaration. [Note: a declaration can be explicitly disambiguated by a nonfunction-style cast, by a = to indicate initialization or by removing the redundant parentheses around the parameter name. ]
[Example:
struct S {
S(int);
};
void foo(double a)
{
S w(int(a)); // function declaration
S x(int()); // function declaration
S y((int)a); // object declaration
S z = int(a); // object declaration
}
—end example]
Similarly other examples following this.