【发布时间】:2015-10-22 10:34:17
【问题描述】:
假设我们有课程A:
class A {
public:
A& func1( int ) { return *this; }
A& func2( int ) { return *this; }
};
和 2 个独立功能:
int func3();
int func4();
现在在这段代码中:
A a;
a.func1( func3() ).func2( func4() );
函数func3() 和func4() 的求值顺序是否已定义?
根据这个答案Undefined behavior and sequence points,其中一个序列点是:
- 在函数调用时(无论函数是否内联),在评估所有函数参数(如果有)之后
在函数体 (
§1.9/17) 中执行任何表达式或语句之前发生。
那么“评估所有函数参数”是否意味着,func3() 必须在 func4() 之前调用,因为 func1() 参数的评估必须在调用 func2() 之前发生?
【问题讨论】:
-
几乎是this的复制品,总之没有。
-
@MattMcNabb 是的,谢谢,已修复
-
类似于Does this code from “The C++ Programming Language” 4th edition section 36.3.6 have well-defined behavior? 中更复杂的示例,但我在回答中提供了很多细节,这可能有助于理解这一点。有很多相互交织的问题。