Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C++ "declaration and initialization" statement, an expression?

The language standard says:

[ Note: Clause 5 defines the syntax, order of evaluation, and meaning of expressions.58 An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects. — end note ]

E.g. I've code below:

int i=1;
A obj;

So, do both statements above, count as "expression"?

Some people on stackoverflow says "int i=1;" is not an expression. This is quite odd to me.

(1) An initialization is a kind of "computation", right? So it should be considered as "expression"?

(2) A obj; //invokes a ctor. A ctor is kind of computation, so it should be considered as "expression"?

like image 849
Hind Forsum Avatar asked Jul 04 '16 02:07

Hind Forsum


1 Answers

That non-normative note in the standard is intended to motivate the concept of an expression, but is not the actual definition. The definition of expression is given in the language grammar which is given in the remainder of clause 5. Expressions are built out of certain terminals such as literals, names of variables, and names of functions, which are combined using operators such as arithmetic and relational operators.

Declarations and expressions are distinct syntactic entities, so a declaration found inside a C++ program is never an expression, and vice versa. The difference is fairly easy to see at a glance: if it declares something, it's a declaration.

1;          // expression statement
int i = 1;  // declaration statement that declares `i`
A(i, 42);   // expression statement that creates an A object
A a(i);     // declaration statement that declares an A object (named a)

A declaration can evaluate expressions but a declaration is not an expression. You rightly point out that the declaration of an object of class type can cause a constructor call. Still it is syntactically a declaration and not an expression.

However, there is another sense in which a declaration is an expression. Namely, rules about the sequencing of evaluations within expressions also apply to declarations. For example, there is a rule that the side effect of a postfix increment on an int takes place at some point before the end of the full-expression.

f(i++) + g();  // i may be incremented before or after g() is called...
h();           // but definitely before h() is called.

For the purposes of such rules, the declaration and initialization of a single variable is considered to also be a full-expression. In the case of a variable of class type, the constructor call is part of that expression.

int i = 1;                      // this declaration evaluates a full-expression
                                // whose effect is to initialize `i` to 1
int j = f(i++) + g(), k = h();  // two separate full-expressions;
                                // i is incremented before h() is called

When reading the standard, you need to consider the context in order to figure out what sense of "expression" is meant.

like image 60
Brian Bi Avatar answered Oct 19 '22 17:10

Brian Bi