链式静态函数调用之间的参数求值顺序

发布于 2024-12-27 15:41:44 字数 1330 浏览 1 评论 0原文

我很好奇为什么链式静态函数和成员函数之间的参数求值顺序存在差异。从这个问题的答案中我可以看到它是未指定的此类链式函数调用之间的参数评估顺序是什么。以以下代码片段为例:

#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),结果输出适合我

5 5 5
3 4 5

但是,我想知道规范中是否有任何原因,或者是否以其他方式知道原因静态函数从左到右(及其参数)计算,对于非静态函数,所有参数首先计算(从我在其他测试中看到的从右到左)。

我问这个问题的原因是,当我尝试在 C 中获得类似的行为(使用结构体和函数指针)并失败时,我第一次注意到这种行为差异。我强烈怀疑这是在 GCC 和 MSVC 中针对成员函数实现的一些优化,但我希望这里有人可以对此进行更多说明。

编辑:
我忘记提及一个令我感到奇怪的关键信息:GCC 只会对链接的非静态函数上的未指定行为发出警告,但不会对静态函数发出警告:

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

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

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;
}

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).

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.

Edit:
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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2025-01-03 15:41:44

没有理由。就像你说的,语言未指定顺序。

使用从右到左顺序的一个原因是,具有可变数量参数的函数(例如 printf)将始终将第一个参数放在顶部。否则没关系。

No reason. Like you say, the order is unspecified by the language.

One reason for using right to left order is that functions with a variable number of parameters, like printf, will then always have the first parameter on top. Otherwise it doesn't matter.

别忘他 2025-01-03 15:41:44

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文