2

I noticed that the constructor with parentheses behaves differently, at least for floats and ints.

// int a, b{}, c = T(), d = T{}, e();
float a, b{}, c = T(), d = T{}, e();
endl(std::cout << a << ", " << b << ", " << c << ", " << d << ", " << e);

// 0, 0, 0, 0, 1

It's not a default constructor, the parentheses were parsed as part of the type. I printed the types, and e has type float (&)() (or just float() with proper forwarding), so I think it's a default-constructed function. The result of calling it has a type, but calling it results in the linker error you'd expect. Its value would be 0 if it were a function pointer. Why would its value be 1, or how does it end up as 1 when it's printed?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
John P
  • 1,463
  • 3
  • 19
  • 39
  • "I think it's a default-constructed function". It is just a function. Nothing is "default-constructed" there and there no need to "construct" anything. – AnT stands with Russia Dec 10 '18 at 16:55
  • I see that from the linker error, but I assumed from the printed value that it had to have some value of its own. I didn't realize I was printing a function pointer. – John P Dec 10 '18 at 16:57
  • As an aside, I made `Wrapper e()` which constructs `int value` with perfect forwarding. `value` is 1. Is that the same thing happening, is `int()` decayed to `int (*)()` and to `bool` before it becomes `int`? I even added `Wrapper::Wrapper(void)`, and only `Wrapper e = Wrapper()` resulted in `value==0`. – John P Dec 10 '18 at 17:06
  • I tried extra parentheses to avoid the function parse, as in `Wrapper (e())`, but the value is still 1. – John P Dec 10 '18 at 17:08

2 Answers2

3
float e();

This declares e as a function that returns float and takes no arguments. So This decays to a function pointer which decays to bool which prints 1 unless boolalpha is set.

balki
  • 26,394
  • 30
  • 105
  • 151
2

It's not a function pointer, it's literally a function (albeit an undefined one).

By streaming the expression e, which decays, you're creating a pointer to that function, and said pointer is valid because it points to the function you've declared called e. Hence, 1.

You're right to say that if you declared a function pointer to begin with, and made it nullptr, you'd see 0 instead by streaming it. But functions and function pointers are two different things.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055