Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it legal to use the increment operator in a C++ function call?

There's been some debate going on in this question about whether the following code is legal C++:

std::list<item*>::iterator i = items.begin(); while (i != items.end()) {     bool isActive = (*i)->update();     if (!isActive)     {         items.erase(i++);  // *** Is this undefined behavior? ***     }     else     {         other_code_involving(*i);         ++i;     } } 

The problem here is that erase() will invalidate the iterator in question. If that happens before i++ is evaluated, then incrementing i like that is technically undefined behavior, even if it appears to work with a particular compiler. One side of the debate says that all function arguments are fully evaluated before the function is called. The other side says, "the only guarantees are that i++ will happen before the next statement and after i++ is used. Whether that is before erase(i++) is invoked or afterwards is compiler dependent."

I opened this question to hopefully settle that debate.

like image 534
Michael Kristofik Avatar asked Feb 28 '09 15:02

Michael Kristofik


People also ask

Can we use increment operator in C?

The decrement (–) and increment (++) operators are special types of operators used in programming languages to decrement and increment the value of the given variable by 1 (one), respectively.

Can you use ++ in C?

In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable.

What is the use of increment operator in C?

In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.

Can we use increment operator in array?

The ++ and -- operators act on scalar values, incrementing or decrementing them. An array (not a pointer) is an aggregate of values (an ordered list of values stored contiguously in memory) so it cannot be incremented or decremented.


1 Answers

Quoth the C++ standard 1.9.16:

When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function. (Note: Value computations and side effects associated with the different argument expressions are unsequenced.)

So it would seem to me that this code:

foo(i++); 

is perfectly legal. It will increment i and then call foo with the previous value of i. However, this code:

foo(i++, i++); 

yields undefined behavior because paragraph 1.9.16 also says:

If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

like image 188
Michael Kristofik Avatar answered Sep 20 '22 20:09

Michael Kristofik