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.
In C++, any expression followed by a semicolon is a legal statement. (Why? Because C let you do this, I think). That means that all of the following are legal statements:
5;
3 + 5;
1 % 2 == 0;
The effect of a statement of this form is that the expression is evaluated and then discarded. A good optimizing compiler would just eliminate all of the logic here, since none of these have any side-effects.
In your case, writing
accept;
is a legal statement because accept
is an expression evaluating to a reference to the function accept
. That means that accept;
as a statement means "evaluate the address of accept
, then discard it." The reason that no function is called here is that a function name by itself doesn't invoke the function; you need the parentheses (the function call operator) to actually make the call. This is useful, for example, if you want to pass a function into another function. For example, you might want to pass a comparison function into std::sort
, like this:
std::sort(range.begin(), range.end(), nameOfMyComparisonFunction)
Here, it would be a real problem if this tried calling nameOfMyComparisonFunction
, since the arguments can't be known until the sorting routine starts off.
So why is this a warning and not an error? Well, it's perfectly legal C++ code, so the compiler can't compliantly call it an error. However, the compiler is right to flag it as a warning, since it almost certainly means you made an error. That said, most compilers have some setting that reports warnings as errors, and if you crank up the warning level high enough the compiler probably would say "this is so suspicious that I'm going to assume you messed something up."
As to your last one - why does
bool a = accept;
end up setting a
to true?
In C++, any non-null pointer implicitly converts to true and any null pointer implicitly converts to false. In C++, functions are implicitly convertible to pointers to themselves, so in this case accept
evaluates to the address of the accept
function, which is non-null, so it sets a
to true
. When you then write
cout << a << endl;
the value is printed as 1, because bool
values are printed as 1 and 0 rather than true
and false
by default. That said, you could write
cout << boolalpha << a << endl;
and you'll see true
printed instead.
Hope this helps!