Why “Foo f(Bar());” can be a declaration of a function that takes type Bar and returns type Foo? [duplicate]

The function f actually takes a function pointer to a function that takes no arguments and gives a Bar. The type of the argument to f is Bar (*)().

This code fails to compile (and we can see the actual type of the argument in the error message):

class Foo { };
class Bar { };

Foo f(Bar());

int main() {
  Bar b;
  f(b);
  return 0;
}

But this code does compile:

class Foo { };
class Bar { };

Foo f(Bar());

Bar g();

int main() {
  f(g);
  return 0;
}

The second meaning it could have, as you say in the question, is that you are making a new Foo object called f and you are calling the constructor with Bar() (a new instance of Bar). It would be similar to:

Foo f = Foo(Bar());

In this situation of Foo f(Bar()); though, the first interpretation is chosen by the compiler.

Somewhat confusingly, if you add another set of parentheses, as in

Foo f((Bar()));

the compiler picks the second interpretation.

Leave a Comment