It's widely-known (though not widely enough >.<) that C and C++ don't specify the order in which parameters to a function are evaluated. That is, the two puts()
s below can occur in either order, as an arbitrary compiler choice:
#include <cstdio>
int Function1() {
std::puts("Function1");
return 1;
}
int Function2() {
std::puts("Function2");
return 2;
}
int Add(int x, int y) {
return x + y;
}
int main() {
return Add(Function1(), Function2());
}
However, does this also apply to the evaluation of this
on the left side of the .
, .*
, ->
or ->*
operators? In other words, is the order of the puts()
's below also unspecified?
#include <cstdio>
struct Struct {
Struct() { std::puts("Struct::Struct"); }
int Member(int x) const { return x * 2; }
};
int Function() {
std::puts("Function");
return 14;
}
int main() {
return Struct().Member(Function());
}
Struct().Member(Function())
This is a function call where the postfix-expression is Struct().Member
and the argument is Function()
. In general, in a function call, the evaluation of the postfix-expression is unsequenced with respect to the evaluation of arguments.
Therefore the order of the puts
calls is indeed unspecified.
It's completely irrelevant that the function is a non-static member function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With