Adding true;
/ false;
is clearly valid C++ code. It compiles and runs just fine.
Similarly, this is the same for statements like int;
, void;
, {}
(no ()), 1+1;
, 1 == 1;
, or even just 1;
... why? (I'm using Visual C++)
Adding true;
/ false;
is clearly valid C++ code. It compiles and runs just fine.
Similarly, this is the same for statements like int;
, void;
, {}
(no ()), 1+1;
, 1 == 1;
, or even just 1;
... why? (I'm using Visual C++)
Why not? The language specification clearly states that expression statement in C++ looks as follows
<expression>;
This is exactly what you have in your examples like true;
or 1 == 1;
or 1;
.
The {}
is just an empty compound statement.
Meanwhile, int;
is ill-formed. If the compiler accepts it quietly, it must be some sort of compiler-specific quirk/bug/extension.
false;
and true;
are valid statements. They evaluate to false
and true
respectively and have no side effects.
However, int;
is not a valid statement. If VS allows it, that is a bug in VS.
{};
is an empty compound or block statement {}
and an empty statemet ;
. Both are legal constructs.
1 == 1;
is a legal statement. It evaluates to true
and has no side effects.
1;
is a legal statement. It evaluates to 1
and has no side effects.
Those are valid lines of code because there is no reason for them not to be - infact, it would have taken extra effort to program them not to be, and the language designers did not want to go through that extra effort. (However, in some languages such as C#, many of these are not valid).
C++ defines a "statement" (something terminated by a semicolon at the end of the line) to be a few particular statements, such as return x
or throw y
, or any expression. Expressions must be allowed so that statements such as foo();
are valid. The language designers could have explicitly defined a statement to include function calls and several other things instead of including just any expression, but it was easier to just say any expression. This covers true;
, false;
, 1+1;
, 1==1;
, 1;
, etc.
In the case of curly braces, it is actually permissible (and sometimes useful) to make curly braces that don't have an associated if
/while
/for
/etc. They declare a new scope. For example, the following prints 2
:
void foo() {
int x = 2;
{
int x = 3;
}
cout<<x<<endl; //prints 2
}
The language specification can be expressed (approximately) as something called a context-free grammar, and you can find more information about this on Wikipedia.
void;
, int;
are ill formed as stated before (lets get these out of the way).
Static analyzers and code checking tools will inform you on the rest of the expressions being pointless or potentially erroneous. Some C++ compilers will even issue a warning.
The reason C++ accepts such things is because the C syntax accepted them as well and this legacy was carried on. On 'why did C accept them then?' the answer is simply that the language had many parts that were "compiler driven" ie made to ease the work of the compiler writer.
There is a great example in the book 'Deep C secrets' titled "the 20million dollars bug" which turned out to be a line:
x == 2;
A totally pointless statement which should have been an assignment; no warnings no error (and no money until they found the bug)
If your function return value 0 or 1 (any number basically ) (true / false ) or expression can be evaluated then it will result into true or false:
for example if you write :
int func() {
return true;
}
while ( func() ) {
...
}
it will use true.