2
#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?

Euphoria
  • 77
  • 7
  • 1
    See [How to print function pointers with cout?](https://stackoverflow.com/questions/2064692/how-to-print-function-pointers-with-cout). There is no "*function call*" involved in the posted code. – dxiv Oct 15 '20 at 04:07
  • 3
    The error produced by g++ is quite informative: https://godbolt.org/z/aePP3c – user4581301 Oct 15 '20 at 04:08
  • 3
    To call a function in C++ you *must* use parentheses. Otherwise (without parentheses) the function decays to a *pointer* to the function, i.e. `&returnFive`. – Some programmer dude Oct 15 '20 at 04:10
  • 1
    you may find your answer here: https://stackoverflow.com/questions/17073066/g-calling-a-function-without-parenthesis-not-f-but-f-why-does-it-alwa – Sakib Ahammed Oct 15 '20 at 04:26
  • 1
    Does this answer your question? [g++ "calling" a function without parenthesis (not f() but f; ). Why does it always return 1?](https://stackoverflow.com/questions/17073066/g-calling-a-function-without-parenthesis-not-f-but-f-why-does-it-alwa) – Sakib Ahammed Oct 15 '20 at 04:26

1 Answers1

1

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).

Wander3r
  • 1,801
  • 17
  • 27