Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the parenthesis operator does on its own in C++

Tags:

c++

During writing some code I had a typo that lead to unexpected compilation results and caused me to play and test what would be acceptable by the compiler (VS 2010).

I wrote an expression consisting of only the parenthesis operator with a number in it (empty parenthesis give a compilation error):

(444);

When I ran the code in debug mode, it seems that the line is simply skipped by the program. What is the meaning of the parenthesis operator when it appears by itself?

like image 957
SIMEL Avatar asked Mar 06 '23 01:03

SIMEL


1 Answers

If I can answer informally,

(444);

is a statement. It can be written wherever the language allows you to write a statement, such as in a function. It consists of an expression 444, enclosed in parentheses (which is also an expression) followed by the statement terminator ;.

Of course, any sane compiler operating in accordance with the as-if rule, will remove it during compilation.

One place where at least one statement is required is in a switch block (even if program control never reaches that point):

switch (1){
case 0:
    ; // Removing this statement causes a compilation error
}
like image 50
Bathsheba Avatar answered Mar 16 '23 22:03

Bathsheba