Why does the compiler interpret this line as a function definition and not as a variable definition:
Y y(X());
in the following code:
#include <iostream>
struct X {
X() { std::cout << "X"; }
};
struct Y {
Y(const X &x) { std::cout << "Y"; }
void f() { std::cout << "f"; }
};
int main() {
Y y(X());
y.f();
}
VS2010 gives the following error on line "y.f();"
left of '.f' must have class/struct/union
Which part of the standard describes this behavior? The answer to the following question doesn't give details about it: Most vexing parse
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.
Most vexing parse problem. Y y(X())
is actually function declaration with name y
, that returns Y
and receives argument of type function, that returns X
and receives nothing.
It's solved in C++11 with usage of {}
for construct object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With