I\'m reading over \"The C++ Programming Language - Fourth Edition\" and I was typing up a simple exercise just to get a hang of C++ syntax and I accidentally stumbled across
There is no "call to accept
". See #3.
Because of #1.
The use of a function name without function call syntax (ie: ()
) means that you're accessing the function itself. You could, for example, store it in a function pointer (through function-to-pointer decaying):
using my_func = bool(*)(); //Function that takes nothing and returns a bool.
my_func var = accept; //Store a pointer to `accept`.
You could then issue var();
, which would call accept
.
However, because you never store the function, the compiler guesses that you probably meant to call the function, not to access the function's pointer. accept;
however is a legal C++ statement, therefore the compiler cannot error on it. It can emit a warning, since the statement accomplishes nothing and you probably meant to call the function. It's no different from a statement like 1;
: perfectly legal, but utterly useless.
It does this because of C++ trickery. Non-null pointers decay to the boolean value true
. And accept
decays to a function pointer that is not null. Therefore, when converted to a bool
, it will be true
. You're still not calling the function.