问题描述
考虑一个 class MyClass
:
- 一个成员函数
myClass& myFunction1(int)
修改
对象并返回* this
-
int myFunction2()const
不会
修改对象
- a member function
myClass& myFunction1(int)
that modifies theobject and returns*this
- a member function
int myFunction2() const
that does notmodify the object
C ++ 11/14标准保证:
Does the C++11/14 standard guarantee that:
myclass.myFunction1(myclass.myFunction2()).myFunction1(myclass.myFunction2());
等效于:
myclass.myFunction1(myclass.myFunction2());
myclass.myFunction1(myclass.myFunction2());
推荐答案
否。编译器可以先调用 myclass.myFunction2()
两次,然后在第一个示例代码中执行两个 myFunction1
调用。但不是在第二个示例代码中。
No. The compiler can first call myclass.myFunction2()
twice and then do the two myFunction1
calls in the first example code. But not in the second example code.
没有什么阻止编译器在函数参数的求值和函数调用之间插入一些东西。只要调用实际发生在调用参数(该函数的)的评估之后。
There is nothing that stops the compiler from sticking something in between the evaluation of function arguments and the call of the function. As long as the call actually happen after the evaluation of the call arguments (of that function). In Standardese terms
不同的表达式通常是无序的(除非有明确的措辞对它们进行排序)。您对 myclass.myFunction2
的两个调用是这样的未排序的情况,因此可以出现对 myclass.myFunction2
的调用之一(和之前调用任何 myFunction1
)。
Different expressions generally are unsequenced (unless there is explicit wording that sequences them). Your two calls to myclass.myFunction2
are such unsequenced cases, so that one of the calls to myclass.myFunction2
can appear after the other (and before the call to any of the myFunction1
).
这篇关于链接操作和对同一对象的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!