0
int res(int n)
{
    return 55;
}

int main(){
  cout<< res;  

  return 0;
}

I expected it to produce an error, but it printed 1.

1 Answers1

0

When you refer to a function by its name alone, you get a pointer to that function.

operator<< does not have an overload that accepts any arbitrary function pointer. It has an overload that accepts a void* pointer (which a function pointer is not implicitly convertible to), and overloads which accept pointers to functions that operate on output streams (which your function does not).

However, operator<< does have an overload that accepts a bool, and there is an implicit conversion defined from a function pointer to a boolean. So, you end up with a boolean value of true (which gets printed as an integer 1 unless you use the std::boolalpha I/O manipulator) because the function pointer is not null.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770