Comma expression is a series of expressions separated by commas. These expressions are evaluated from left to right. The result of a comma expression is the value of the rightmost expression. In the following example, the value of the first comma expression is ix; the value of the second is 0.
// ia, sz, and index are defined elsewhere ... int ival = (ia != 0) ? ix=get_value(), ia[index]=ix : ia=new int[sz], ia[index]=0;
For %
operator, When both operands are positive, the
result is positive. If either (or both) operand is negative,
however, the sign of the remainder is machine-dependent.
; // null statement {} // equivalent of null statementThe accidental presence of a superfluous null statement does not generate a compile-time error.
i = d + s;; // ok: superfluous null statement
In the 1970s, computer program language design philosophy emphasized the virtue of defining all objects at the start of the program, function or statement block prior to any program statements. (In C, for example, the definition of an object is not treated as a language statement and all object definitions within a block must appear before any program statements. By necessity, C programmers habituate themselves to defining all objects at the top of each current block.) In part, this was a reaction to an error-prone idiom of object definition on-the-fly supported by FORTRAN.
In C++, because the definition of an object is a statement of the language, object definitions in general can be placed anywhere that the other statements of the language can appear. Syntactically, this is what makes locality of declaration possible.
For the built-in types, such as integers and floats, loçality of declaration is primarily a matter of personal preference. The language encourages it by allowing declarations to occur within the condition part of the if, else-if, switch, while, and for loop. Those who favor locality of declaration believe that it makes for more easily understood programs.
Locality of declaration becomes necessary with the definition of class objects with associated constructors and a destructor. When place these class objects at the beginning of a function or statement block, two things happen:case
or default
label is illegal unless
it is placed within a statement block. For example, the following
results in a compile-time error:
case illegal_definition: string s = get_file_name(); // error: declaration must within statement block // ... break;If the definition not enclosed within a statement block, it would be visible across case labels but initialized only if the case label within which it is defined is executed. Requiring a statement block ensures that the name is visible and therefore can be used only where it is guaranteed to have been initialized.
case ok: { string file_name = get_file_name(); // ok: declaration within statement block // ... break; }Index