How does the compiler determine what value to output during runtime when a function name is sent to cout without parenthesis? C++

Viewed 56
#include <iostream>

int returnFive()
{
   return 5;
}

int main()
{
   std::cout << returnFive << '\n';
   return 0;
}

Since this compiles without error, how does the system determine what value is actually sent and printed to console?

1 Answers

Imagine if the code written is something like

if(returnFive)
  returnFive();

Here the expectation is that the compiler checks if the function pointer for returnFive is nullptr or not.

The compiler here is evaluating the function pointer as a boolean expression of whether it is NULL or not and printing the output.

https://godbolt.org/z/Psdc69. You can check that the cout is being passed a (bool).

Related