链接操作和对同一对象的评估顺序 [英] Chaining operation and order of evaluation on the same object

查看:118
本文介绍了链接操作和对同一对象的评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个 class MyClass


  • 一个成员函数 myClass& myFunction1(int)修改
    对象并返回 * this

  • int myFunction2()const 不会
    修改对象

  • a member function myClass& myFunction1(int) that modifies the object and returns *this
  • a member function int myFunction2() const that does not modify 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


当调用函数(无论函数是否为内联函数)时,每个值计算和与任何参数表达式相关的副作用,或者指定被调用函数的后缀表达式,在被调用函数体中的每个表达式或语句执行之前被排序。

When calling a function (whether or not the function is inline), every value computation and side effect associated with any argument expression, or with the postfix expression designating the called function, is sequenced before execution of every expression or statement in the body of the called function.

不同的表达式通常是无序的(除非有明确的措辞对它们进行排序)。您对 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).

这篇关于链接操作和对同一对象的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆