【问题标题】:Is the evaluation of a function call's "this" in an unspecified order relative to the parameters?是否以相对于参数的未指定顺序评估函数调用的“this”?
【发布时间】:2015-10-15 18:19:30
【问题描述】:

众所周知(尽管不够广泛>.puts()s 可以按任意顺序出现,作为任意编译器选择:

#include <cstdio>

int Function1() {
    std::puts("Function1");
    return 1;
}

int Function2() {
    std::puts("Function2");
    return 2;
}

int Add(int x, int y) {
    return x + y;
}

int main() {
    return Add(Function1(), Function2());
}

但是,这是否也适用于 ..*-&gt;-&gt;* 运算符左侧的 this 的求值?换句话说,puts()下面的顺序未指定吗?

#include <cstdio>

struct Struct {
    Struct() { std::puts("Struct::Struct"); }
    int Member(int x) const { return x * 2; }
};

int Function() {
    std::puts("Function");
    return 14;
}

int main() {
    return Struct().Member(Function());
}

【问题讨论】:

标签: c++ parameter-passing this


【解决方案1】:
Struct().Member(Function())

这是一个函数调用,其中后缀表达式Struct().Member,参数是Function()。通常,在函数调用中,后缀表达式的计算相对于参数的计算是无序的。

因此,puts 调用的顺序确实未指定。

函数是非静态成员函数是完全不相关的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多