Why is the "truth"? (and others) a valid line of C ++ code?

Adding true; / false; is explicitly valid C ++ code. It compiles and works fine.

Similarly, for operators such as int; , void; , {} (no ()), 1+1; , 1 == 1; or even just 1; ... why? (I am using Visual C ++)

+4
source share
5 answers

Why not? The language specification clearly states that the expression expression in C ++ is as follows

 <expression>; 

This is exactly what you have in your examples, such as true; or 1 == 1; or 1; .

{} is just an empty compound statement.

Meanwhile int; poorly formed. If the compiler takes it quietly, it must be some kind of compiler specific quirk / bug / extension.

+4
source

false; and true; are valid operators. They evaluate to false and true respectively, and have no side effects.

However, int; not correct. If VS allows this, this is an error in VS.

{}; - it is an empty compound or block operator {} and an empty statemet ; . Both are legal constructs.

1 == 1; is a legal expression. It evaluates to true and has no side effects.

1; is a legal expression. It scores 1 and has no side effects.

+1
source

These are valid lines of code, because there is no reason for them not to be - infact, it would require additional efforts to program them so that they were not, and the language developers did not want to carry out these additional efforts. (However, in some languages, such as C #, many of them are unacceptable).

C ++ defines a “statement” (something ending with a semicolon at the end of a line) to be several specific statements, such as return x or throw y , or any expression. Expressions must be allowed so that statements such as foo(); executed foo(); . Language developers could explicitly define an operator that includes function calls and a few other things, instead of just including an expression, but it was easier to just say an 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 have no associated if / while / for / etc. They are announcing a new scale. For example, the following prints 2 :

 void foo() { int x = 2; { int x = 3; } cout<<x<<endl; //prints 2 } 

A language specification can be expressed (approximately) as something called context-free grammar, and you can find more information about it on Wikipedia.

+1
source

void; , int; poorly formed, as indicated earlier (let's get rid of them).

Static analyzers and code verification tools will inform you that other expressions are meaningless or potentially erroneous. Some C ++ compilers even give a warning.

The reason C ++ accepts such things is because the C syntax also accepted them , and this legacy has been continued. "Why did C accept them then?" the answer is that the language had many parts that were “controlled by the compiler”, that is, made to facilitate the work of the compiler writer.

There ’s a great example in the book Deep C secrets called the $ 20 Million Error, which turned out to be a ruler:

 x == 2; 

A completely meaningless statement, which was to be an appointment; no warnings no error (and no money until they detect an error)

+1
source

If your function returns a value of 0 or 1 (any number basically) (true / false) or the expression can be evaluated, then this will lead to true or false:

for example, if you write:

 int func() { return true; } while ( func() ) { ... } 

he will use true.

0
source

All Articles