2

Гарантирована ли последовательность действий в for:

for (int i = 0, j = 0; i < size; ++i, j = i * coaf) {

тобишь, гарантировано ли то, что ++i будет расчитано перед тем как рассчитать j = i * coaf?

2

2 ответа 2

5

Да, но к самому for это отношения никакого не имеет, т.к. третий его блок это любое выражение. Это гарантия запятой.

4

for в данном контексте не принципиально, главное в том, что выражение ++i, j = i * coaf включает в себя оператор запятой, а у этого оператора все побочные эффекты от вычисления левой стороны происходят до любых вычислений правой стороны:

8.5.19 Comma operator [expr.comma]
1 The comma operator groups left-to-right.
expression:
assignment-expression
expression , assignment-expression
A pair of expressions separated by a comma is evaluated left-to-right; the left expression is a discarded-value expression (8.2). Every value computation and side effect associated with the left expression is sequenced before every value computation and side effect associated with the right expression. The type and value of the result are the type and value of the right operand; the result is of the same value category as its right operand, and is a bit-field if its right operand is a bit-field. If the right operand is a temporary expression (15.2), the result is a temporary expression.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.