函数调用的 "this" 的评估是否以相对于参数的未指定顺序进行?
Is the evaluation of a function call's "this" in an unspecified order relative to the parameters?
众所周知(尽管还不够广泛 >.<),C 和 C++ 不指定计算函数参数的顺序。也就是说,下面的两个 puts()
可以任意顺序出现,作为任意编译器选择:
#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());
}
但是,这是否也适用于 .
、.*
、->
或 ->*
运算符左侧的 this
的计算?也就是说,下面的puts()
的顺序是不是也未指定?
#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())
这是一个函数调用,其中 后缀表达式 是 Struct().Member
,参数是 Function()
。通常,在函数调用中,postfix-expression 的求值相对于参数的求值是无序的。
因此 puts
调用的顺序确实未指定。
函数是非静态成员函数完全无关
众所周知(尽管还不够广泛 >.<),C 和 C++ 不指定计算函数参数的顺序。也就是说,下面的两个 puts()
可以任意顺序出现,作为任意编译器选择:
#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());
}
但是,这是否也适用于 .
、.*
、->
或 ->*
运算符左侧的 this
的计算?也就是说,下面的puts()
的顺序是不是也未指定?
#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())
这是一个函数调用,其中 后缀表达式 是 Struct().Member
,参数是 Function()
。通常,在函数调用中,postfix-expression 的求值相对于参数的求值是无序的。
因此 puts
调用的顺序确实未指定。
函数是非静态成员函数完全无关