Because evaluation order is not specified.
The compiler may generate the code to call refcube(x)
and compute its value before or after generating the code to obtain the value of x
, for output to std::cout
.
Your compiler chose to compile this C++ code as calling refcube(x)
first, and then by evaluating x
.
The C++ language specification spends quite a few pages on the topic of "sequencing", which (very loosely speaking) specifies the evaluation order. To make a long story short, in this example, x
is not sequenced with respect to refcube(x)
, in the expression that generates the output to std::cout
, and such a compiler is free to compile either portion of the expression first.