Evaluation order defines in which order operands of an operator are evaluated. Before C++17, most operators had unspecified evaluation order, leading to undefined behavior in code like f(a++) + f(b++) where the order of side effects was unpredictable. C++17 specified the order for most operators, eliminating many pitfalls.
Don't write code that depends on evaluation order; prefer writing expressions with clear, sequential intent.
This example shows evaluation order issues and how to avoid them.
// compile: g++ -std=c++17 -o eval eval.cpp // run: ./eval // description: evaluation order pitfalls and safe patterns #include <iostream> int counter = 0; int inc() { std::cout << "inc called\n"; return ++counter; } int main() { counter = 0; // In C++17+, left-to-right order for assignment int a = inc() + inc(); // evaluates left to right // Avoid unclear orders: use separate statements int x = inc(); int y = inc(); std::cout << "counter = " << counter << "\n"; std::cout << "a = " << a << ", x = " << x << ", y = " << y << "\n"; return 0; }