# Unraveling the Mystery: Understanding Function Argument Evaluation Order in C++

Let's explore **"Don’t depend on the order of evaluation of function arguments"**. You might be wondering what I'm talking about because many developers assume that function arguments are evaluated from left to right. That's incorrect! There are no guarantees!

**Reason:** The order is unspecified.

**Note:** C++17 has stricter rules for the order of evaluation, but the order of function argument evaluation is still unspecified.

Let's look at a simple example:

```c++
//test.cpp

#include <iostream>

void foo(int a, int b) {
    std::cout << "a: " << a << " " << "b: " << b << std::endl;
}

int main() {
    int i = 0;
    foo(i++, i++); // (1, 0) | (0, 1) | (0, 0)

    return 0;
}
```

If you understand what I'm saying, then you're on the right track. If not, let's move on to the explanation.

Here's my proof. The output from gcc and clang is different:

* gcc
    

`g++ -std=c++2a -O3 -Wall test.cpp -o test && ./test`

```bash
test.cpp: In function ‘int main()’:
test.cpp:9:15: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    9 |     foo(i++, i++); // (1, 0) | (0, 1) | (0, 0)
      |              ~^~
test.cpp:9:15: warning: operation on ‘i’ may be undefined [-Wsequence-point]
a: 1 b: 0
```

* clang
    

`clang++ -std=c++2a -O3 -Wall test.cpp -o test && ./test`

```bash
test.cpp:9:10: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
    foo(i++, i++); // (1, 0) | (0, 1) | (0, 0)
         ^    ~~
1 warning generated.
a: 0 b: 1
```

The function call could be evaluated as `foo(0, 1)` or `foo(1, 0)`, but the order is not guaranteed. Technically, the behavior is undefined. In C++17, this code doesn't have undefined behavior, but it still doesn't specify which argument is evaluated first.

I compile in C++20 or use the best error detection during compilation with optimization by using a compiler flag.

If you want to learn more about this, check out these links:

* [Why doesn't C++ have a specified order for evaluating function arguments?](https://isocpp.org/blog/2016/08/quick-q-why-doesnt-cpp-have-a-specified-order-for-evaluating-function-argum)
    
* [Evaluation order in C++](https://en.cppreference.com/w/cpp/language/eval_order)
    
* [Compilers and argument order of evaluation in C++](https://stackoverflow.com/questions/621542/compilers-and-argument-order-of-evaluation-in-c)
