C++函数调用中参数括号()的结合性

Associativity of parameters parentheses () in C++ function call

这里是示例代码

#include "stdafx.h"
#include <iostream>

int func_A();
int func_B();
void func_C(int a, int b);


int main()
{
    func_C(func_A(), func_B());
    return 0;
}

int func_A()
{
    std::cout << "in Function A" << std::endl;
    return 1;
}

int func_B()
{
    std::cout << "in Function B" << std::endl;
    return 2;
}

void func_C(int x, int y)
{
    std::cout << x + y;
}

输出: 在函数 B 在函数 A 3

为什么 func_B 先被调用?我在 c# 中尝试了相同的程序,其中 func A 首先被调用。

C++ 标准声明不能保证函数调用参数中的哪一个语句将首先执行。这由编译器的优化器决定。所以你不应该依赖它。

即使在两个不同的调用中,它也可能不同。此外,如果您现在编译代码并且它按预期工作,则不能保证它在下一个构建或同一编译器的下一个版本中也能正常工作。

但正如 Martin 在评论中提到的那样:"C# on the other hand, does mandate the order of evaluation"