简体   繁体   English

首先评估哪个默认参数,为什么?

[英]Which default argument is evaluated first and why?

I have three functions, funt1() , funt2() , and funt3() . 我有三个函数, funt1()funt2()funt3()

int funt1()
{
    cout<<"funt1 called"<<endl;
    return 10;
}

int funt2()
{
    cout<<"funt2 called"<<endl;
    return 20;
}

void funt3(int x=funt1(), int y=funt2())
{
    cout << x << y << endl;
}

My main function: 我的main功能:

int main()
{
    funt3();
    return 0;
}

When I am calling funt3() in my main() method, why is funt1() is called first, and then funt2() ? 当我在main()方法中调用funt3()时,为什么先调用funt1() ,然后funt2()

It depends on your compiler. 这取决于你的编译器。 Others may call funct2() first. 其他人可能先调用funct2() Neither C or C++ guarantee the order of evaluation of function arguments. C或C ++都不保证函数参数的评估顺序。

See Parameter evaluation order before a function calling in C 在C中调用函数之前,请参阅参数评估顺序

C++ standard does not define that, so it's totally compiler-specific. C ++标准没有定义它,所以它完全是特定于编译器的。 That said, you should never rely on an instance of undefined behaviour. 也就是说,你永远不应该依赖未定义行为的实例。

EDIT: if you really want to keep functions invocations as default parameters to reduce the numbers of parameters you have to pass each time I suggest you do the following: 编辑:如果你真的想保持函数调用作为默认参数,以减少每次我必须通过的参数数量,我建议您执行以下操作:

void funt3(int x, int y)
{
    cout<<x<<y<<endl;
}

void funt3(int x)
{
    funt3(x, funt2());
}

void funt3()
{
    funt3(funt1());
}

The language does not require any particular order. 该语言不需要任何特定的顺序。 The order used will be compiler dependent. 使用的顺序将取决于编译器。

compiler dependent. 编译器依赖。 it maybe funt1 then funt2 then funt3 or any other combination. 它可能是funt1然后funt2然后funt3或任何其他组合。

As noted by everybody else, the order of evaluation of function parameters is unspecified by the C++ standard. 正如其他人所指出的那样,C ++标准没有规定功能参数的评估顺序。 This allows each compiler to choose an optimal order, whether that order is determined by convenience or efficiency. 这允许每个编译器选择最佳顺序,无论该顺序是由方便还是效率决定的。

You may even find that the order can change based on the optimization flags you give to your compiler. 您甚至可能会发现订单可以根据您为编译器提供的优化标志进行更改。

In order to guarantee the sequence of the function calls you need to introduce a sequence point between the calls. 为了保证函数调用的顺序,您需要在调用之间引入一个序列点 I accomplish this below by creating two different versions of the function, so that only one function call is made as a parameter. 我通过创建函数的两个不同版本来实现此目的,因此只有一个函数调用作为参数。

void funt3(int x, int y=funt2())
{
    cout << x << y << endl;
}

void funt3()
{
    int x = funt1();
    funt3(x);
}

It is because the parameters of funt3 (sp?) needs to work out x then y . 这是因为funt3 (sp?)的参数需要算出x然后y ie funt1() then funt2() before considering the contents of funt3 . 即在考虑funt3的内容之前funt1()然后funt2()

Compiler specific and from there it goes down to the CPU. 编译器具体,从那里下载到CPU。 CPU might branch that call into separate branches, it might try to do some predictions, or if the CPU thinks func1 is way faster than func2 it will run func1 and a bunch of other operations before func2 so it optimizes. CPU可能会将该调用分支到单独的分支中,它可能会尝试做一些预测,或者如果CPU认为func1比func2快,它将在func2之前运行func1和一堆其他操作,以便进行优化。

As C++ standard doesn't define the order so it's depend on compiler. 由于C ++标准没有定义顺序所以它依赖于编译器。

You can simply try a few popular c++ compilers: GCC, VS2008/VS2010 etc. Then you will see totally different result. 你可以简单地尝试一些流行的c ++编译器:GCC,VS2008 / VS2010等。然后你会看到完全不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪个布尔表达式首先在包含OR操作的IF语句中求值? - Which boolean expression gets evaluated first in an IF statement containing an OR operation? 为什么cout的默认精度不会影响评估结果? - Why cout's default precision doesn't effect evaluated result? 为什么默认参数构造函数被称为默认构造函数 - Why default argument constructor is called as default constructor function template:默认第一个模板参数为second - function template: default first template argument to second 为什么默认参数不能依赖于非默认参数? - why can't default argument depend on non-default argument? 模板化C ++函数,第一个参数设置为第二个默认参数 - Templated C++ function with the first argument set to the second as default argument 不可能:将此指针作为默认参数。 为什么? - Not possible: this pointer as a default argument. Why? 为什么 lambda 参数的默认参数会触发“-pedantic” GCC 警告? - Why does a default argument for a lambda argument trigger a -pedantic" GCC warning? 如何设置float *参数的默认值,该值应该是指向数组的指针 - How to set a default value of float* argument which should be a pointer to array 为什么在C ++中将-1/2评估为0,而在Python中将-1评估为-1? - Why is -1/2 evaluated to 0 in C++, but -1 in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM