简体   繁体   English

在c ++中std :: cout的奇怪行为

[英]strange behavior of std::cout in c++

#include <iostream>

int a(int &x) {
    x = -1;
    return x;
}

int main () {
    int x = 5;
    std::cout << a(x) << " " << x << std::endl;
}

Why output is "-1 5"? 为什么输出是“-1 5”?

PS: compiler is: PS:编译器是:

i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) i686-apple-darwin11-llvm-g ++ - 4.2(GCC)4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11.00)

PS: compiled without any optimization. PS:没有任何优化编译。

In this line: 在这一行:

std::cout << a(x) << " " << x << std::endl;

the order of evaluation of a(x) and x is not specified. 未指定a(x)x的评估顺序。 It's unspecified behaviour what happens, in your case the compiler decided to evaluate x first, a(x) afterwards. 这是未指定的行为,在您的情况下,编译器决定先评估x ,然后再评估a(x)

The order, in which a(x) and x are being evaluated is unspecified [1] . 正在评估a(x)x的顺序是未指定的 [1] To make sure that x will not be modified within the same expression (and avoiding unspecified behavior by doing so), you could do: 为了确保不会在同一个表达式中修改x (并通过这样做避免未指定的行为),您可以:

int x = 5;
int y = a(x);
std::cout << y << " " << x << std::endl;

[1] "Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. " 5 Expressions, §4 [1] “除非另有说明,否则单个运算符的操作数和个别表达式的子表达式的评估顺序以及副作用发生的顺序是未指定的。” 5表达式,§4

The order of evaluation is unsequenced and hence it is unspecified behavior either a(x) or x could be evaluated first. 评估顺序是排序的,因此它是未指定的行为,或者可以首先评估a(x)x With respect to the C++ draft standard section 1.9 Program execution paragraph 15 says( emphasis mine ): 关于C ++草案标准1.9程序执行15段说( 强调我的 ):

[...] Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced . [...] 除非另有说明,否则对个体操作员的操作数和个别表达式的子表达式的评估是不确定的 [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. [注意:在程序执行期间不止一次评估的表达式中,不需要在不同的评估中一致地执行对其子表达式的未序列和不确定顺序的评估。 —end note ] [...] - 注意事项] [...]

and it may even differ between different evaluations and furthmore if we go back to paragraph 13 it says: 它甚至可能在不同的评估之间有所不同,如果我们回到第13段它会说:

[...] If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced . [...] 如果A在B之前没有排序,B在A之前没有排序,那么A和B都没有排序 [ Note: The execution of unsequenced evaluations can overlap. [注意:未经测试的评估的执行可能会重叠。 —end note ] Evaluations A and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A, but it is unspecified which .[...] -end note]评估A和B是不确定的顺序,当A在B之前测序或B在B之前测序,但未指定哪个 。[...]

which explains that this is unspecified behavior. 这解释了这是未指明的行为。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM