链接的静态函数调用之间的参数评估顺序 [英] Argument evaluation order between chained static function calls

查看:126
本文介绍了链接的静态函数调用之间的参数评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,为什么链静态函数和成员函数之间的参数评估顺序有所不同.从此问题的答案中,我看到它是未指定的在这些链接函数调用之间,参数评估顺序是什么.以下面的代码段为例:

I am curious why there is a difference in the argument evaluation order between chained static functions and member functions. From the answers at this question I can see it is unspecified what the argument evaluation order is between such chained function calls. Take for example the following snippet:

#include <iostream>
class test {
public:
    static test& chain_s(test& t, int i) {
        std::cout << i << " ";
        return t;
    }

    test& chain(test& t, int i) {
        std::cout << i << " ";
        return *this;
    }
};

int main(int, char**) {
    int x = 2;
    test t;
    t.chain(t,++x).chain(t,++x).chain(t,++x);
    x = 2; std::cout << std::endl;
    t.chain_s(t,++x).chain_s(t,++x).chain_s(t,++x);

    return 0;
}

对于GCC 4.6.2和CL 15.00.30729.01(MSVC 9),结果输出是给我的

In the case of GCC 4.6.2 and CL 15.00.30729.01 (MSVC 9) the resulting output is for me

5 5 5
3 4 5

但是,我想知道规范中是否有任何原因,或者是否知道为什么要对静态函数从左到右(使用它们的参数)求值,对于非静态函数,首先要使用所有参数(从我在其他测试中看到的内容从右到左).

However, I was wondering if there is any reason in the specification or if it is otherwise known why the static function are evaluated left-to-right (with their arguments), and for the non-static function all the arguments first (right-to-left from what I've seen in other tests).

我问这个问题的原因是因为当我尝试在C中使用相似的行为(使用结构和函数指针)时,我首先注意到了行为上的这种差异,但失败了.我强烈怀疑这是在GCC和MSVC中为成员函数实现的一些优化,但是我希望这里的人可以对此有所了解.

The reason I'm asking this is because I first noticed this difference in behavior when trying to get similar behavior in C (using a struct and a function pointer) and failed. I strongly suspect this is some optimization implemented both in GCC and MSVC for member functions, but I hope someone here can shed a little more light on this.

修改:
我忘了提到一些至关重要的信息,这让我感到奇怪:GCC只会警告链式非静态函数的未指定行为,而不会警告静态函数:


I forgot to mention one crucial bit of information which strikes me as odd: GCC will only warn on unspecified behavior on the chained non-static function, but not the static functions:

a.cpp: In function 'int main(int, char**)':
a.cpp:18:45: warning: operation on 'x' may be undefined [-Wsequence-point]

GCC没有义务提供此类警告,因此它可能会漏掉第二个表达,但这就是让我相信有趣的事情正在发生的原因.

GCC is not obligated to provide such warnings so it could miss the second expression, but this is what leads me to believe something interesting is going on.

推荐答案

您的代码具有未定义的行为,但我想您知道这一点.还, 您可以根据优化标志轻松看到差异.但 在这种情况下,一个可能的原因是非静态功能需要 三个参数,包括上一个调用的结果,其中 静态函数只需要两个,前一个的结果 呼叫将被忽略.

Your code has undefined behavior, but I suppose you know that. Also, you could easily see a difference depending on optimization flags. But in this case, one likely reason is that the non-static functions require three arguments, including the results of the previous call, where as the static functions only require two, and the results of the previous call are ignored.

这篇关于链接的静态函数调用之间的参数评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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