奇怪的输出,不符合预期 [英] Strange output, not as expected

查看:91
本文介绍了奇怪的输出,不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉问您一个愚蠢的问题,但是我无法弄清楚为什么我继续得到这个输出。
这是我的代码:

sorry for asking you a stupid question, but I just can't figure out why I keep on getting this output. So here is my code:

#include <cstdio>
#include <iostream>

using namespace std;
unsigned n = 4242;

int getRemainderOf(int m, int n, int& quotient);

static int l = 0;   
int main()
{

    int quotient;  // the value of which should be changed after calling the func.

    for(int i=-1; i<=1; ++i)
    {
        for(int j=-1; j<=1; ++j)
        {
            if( i && j )
            {

                cout << "("<< i*3 << "," << j*7 << ") "  <<( getRemainderOf(i*3, 7*j, quotient) ) << " " << quotient <<endl;

                cout << "("<< i*7 << "," << j*3 << ") "  << getRemainderOf(i*7, 3*j, quotient) << " "; cout << quotient <<endl;
            }
        }
    }

    return 0;
}

int getRemainderOf(int m, int n, int& quotient)
{
    ++l;
    cout << l <<endl;
    quotient = m / n;
    cout << " quotient " << quotient <<endl;
    return m % n;
}

所以我期望在输出的第一行看到的是剩余的然后调用函数getRemainderOf()后得到的商。但是当我像这样指出商的值时,我看到商的值是一个垃圾值。因此,即使我已使用引用将变量的值传递给函数,该变量的值也不会更改。
有趣的是,如果我分别指出余数(通过调用函数得到)和商,我会做对的。
我看到问题可能出在调用函数作为运算符<<的参数。函数,但是我不明白为什么商号的值没有改变,因为我在输出函数之前先调用了该函数该运算符的关联性是从左到右的,那么怎么了?
那么,请您告诉我此输出的原因是什么。

so what I expected to see in the first line of my output was the remainder and then the quotient that I get after calling the function getRemainderOf(). But instead when I cout the value of quotient like that I see that the value of quotient is a garbage value. So the value of the variable is not changed even though I've passed it to the function by using reference. The funny thing is that if I cout the remainder (got by calling the function) and the quotient separately I will get it right. I see that the problem might be in calling the function as a argument of the operator << function but I don't get it why the value of the quotient isn't changed since I call the function before I output it. This operator's associativity is left-to-right so what's wrong? So could you please tell me what is the reason of this output.

推荐答案

刚刚发现的一个典型例子就是C ++怪癖之一。您的程序实际上可以分解为以下简单示例:

What you've just found is a classic example of one of the quirks of C++. Your program can actually be decomposed into this simple example:

int i = 10;
f(i, ++i);

编译器可以从左到右选择函数参数评估,但这不能保证。以下是一些标准文本:

The compiler has the choice of function argument evaluation from left-to-right, but this is not guaranteed. Here's some standard text:


5.2 / 4后缀表达式[expr.post]


当调用函数时,每个参数应使用其相应的参数进行初始化。 [注意:这样的初始化彼此之间不确定地排序(1.9)-结束注释]

When a function is called, each parameter shall be initialized with its corresponding argument. [Note: Such initializations are indeterminatly sequenced with respect to each other (1.9) - end note]


由于它们不确定地排序,因此编译器可以自由地在第一个参数之前评估 ++ i 并使用相应的参数对其进行初始化。函数参数,然后求值 i 并随后使用其各自的参数对其进行初始化。

Because they are indeterminatly sequenced, the compiler has the freedom of evaluating ++i before the first argument and initializing it with the corresponding function parameter, and then evaluating i and initializing it with its respective parameter next.

在这里适用原因函数调用参数评估的歧义是因为 operator<<(()是一个函数,但是它只是用运算符语法调用的。例如,如果我们使用运算符ID语法,这就是您的代码:

The reason function call argument evaluation ambiguity is applicable here is because operator<<() is a function but it's just being called with the operator syntax. For example, this is what your code looks like if we use the operator-id syntax:

std::operator<<(std::operator<<(std::operator<<(std::cout, "(").operator<<(i*3), ",").operator<<(j*7), ")").operator<<(getRemainderOf(i*3, 7*j, quotient)).operator<<(quotient);

这些只是带有参数的函数调用链,它们遵循与上述规则相同的规则。

These are just chains of function calls with arguments and they obey the same rule as the one above.

解决方案是对修改对象并在 operator<<(())调用中使用该对象进行排序。您已经通过将 operator<<<()调用划分为两个用分号; 的语句来实现这一点,因此确保在打印之前调用该函数。

The solution is to sequence the act of modifying the object and using it in the operator<<() call. You already achieved this with partitioning the operator<<() call into two statements with the semicolon ;, so the function is guaranteed to be called before quotient is printed.

这篇关于奇怪的输出,不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆