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
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!