Consider this:
float foo( int () )
This declares a function foo (accepts a function that returns an int ) that returns a float .
Now read
Y y(X());
as y as a function (receiving a function returning X ) returning y
The problem arises due to C ++ most unpleasant analysis
Can be solved with
Y y{ X() };
or
Y y( ( X() ) );
Update based on edit:
Quote from the standard:
Β§ 8.2 Resolution of ambiguity [dcl.ambig.res]
1 - Uncertainty arising from the similarity between a function-style cast and a declaration referred to in 6.8 may also arise in the context of a declaration. In this context, the choice is between declaring a function with an excessive set of parentheses around the parameter name and declaring an object using a style function as an initializer. In the same way as for the ambiguities mentioned in 6.8 , permission should consider any construction that may be a declaration of declaration. [Note: declarations can be explicitly eliminated using an inefficient style using a = to indicate initialization or removal of redundant parentheses around the parameter name. ]
[Example: struct S { S(int); }; void foo(double a) { S w(int(a));
Similar to other examples following this.
source share