C++函数调用中参数括号()的相关性

Sud*_*r S 3 c++ operator-precedence

这是示例代码

#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;
}
Run Code Online (Sandbox Code Playgroud)

输出:在功能B中在功能A 3中

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

Meh*_*dad 7

C++标准规定,不能保证首先执行函数调用参数中的哪一个语句.这对编译器的优化器是可以决定的.所以你不应该依赖它.

即使在两个不同的电话中,它也可能不同.此外,如果您现在编译代码并且它按预期工作,则无法保证它在下一个版本或同一编译器的下一个版本中的工作方式相同.

但正如马丁在评论中提到的那样:"另一方面,C#确实要求评估的顺序"