为什么 cout <<不从左到右打印输入?

发布于 2024-08-19 09:03:45 字数 384 浏览 5 评论 0 原文

以下代码:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue() << myQueue.dequeue();

将“ba”打印到控制台

同时:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue();
cout << myQueue.dequeue();

打印“ab” 这是为什么?

似乎 cout 首先调用最外层(最接近 ;) 函数并按其方式工作,这是它的行为方式吗?

the following code:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue() << myQueue.dequeue();

prints "ba" to the console

while:

myQueue.enqueue('a');
myQueue.enqueue('b');
cout << myQueue.dequeue();
cout << myQueue.dequeue();

prints "ab" why is this?

It seems as though cout is calling the outermost (closest to the ;) function first and working its way in, is that the way it behaves?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温馨耳语 2024-08-26 09:03:45

<< 运算符没有序列点,因此编译器可以首先评估任一 dequeue 函数。可以保证的是,第二个 dequeue 调用的结果(按照它在表达式中出现的顺序,而不一定是它的求值顺序)是 <<< /code>' 为 <<' 第一个的结果(如果你明白我的意思)。

所以编译器可以自由地将你的代码翻译成类似这些的东西(伪中间c++)。这并不是一份详尽的清单。

auto tmp2 = myQueue.dequeue();
auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
auto tmp2 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
auto tmp2 = myQueue.dequeue();
tmp3 << tmp2;

这是临时变量在原始表达式中对应的内容。

cout << myQueue.dequeue() << myQueue.dequeue();
|       |               |    |               |
|       |____ tmp1 _____|    |_____ tmp2 ____|
|                       |
|________ tmp3 _________|

There's no sequence point with the << operator so the compiler is free to evaluate either dequeue function first. What is guaranteed is that the result of the second dequeue call (in the order in which it appears in the expression and not necessarily the order in which it is evaluated) is <<'ed to the result of <<'ing the first (if you get what I'm saying).

So the compiler is free to translate your code into some thing like any of these (pseudo intermediate c++). This isn't intended to be an exhaustive list.

auto tmp2 = myQueue.dequeue();
auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
auto tmp2 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
tmp3 << tmp2;

or

auto tmp1 = myQueue.dequeue();
std::ostream& tmp3 = cout << tmp1;
auto tmp2 = myQueue.dequeue();
tmp3 << tmp2;

Here's what the temporaries correspond to in the original expression.

cout << myQueue.dequeue() << myQueue.dequeue();
|       |               |    |               |
|       |____ tmp1 _____|    |_____ tmp2 ____|
|                       |
|________ tmp3 _________|
べ繥欢鉨o。 2024-08-26 09:03:45

示例中的调用:

cout << myQueue.dequeue() << myQueue.dequeue();

转换为以下表达式,其中两次调用 operator<< 函数:

operator<<( operator<<( cout, myQueue.dequeue() ), myQueue.dequeue() );
-------------------- 1
---------2

coutmyQueue.dequeue() 的求值顺序 未指定。但是,operator<< 函数调用的顺序已明确指定,如 12 标记

The call from your example:

cout << myQueue.dequeue() << myQueue.dequeue();

translates to the following expression with two calls of operator<< function:

operator<<( operator<<( cout, myQueue.dequeue() ), myQueue.dequeue() );
-------------------- 1
---------2

The order of evaluation of cout, myQueue.dequeue() is unspecified. However, the order of operator<< function calls is well specified, as marked with 1 and 2

故事灯 2024-08-26 09:03:45

自 C++17 起,此代码的行为发生了变化; << 的左操作数在 << 的右操作数之前排序,即使它是重载运算符也是如此。现在输出必须是ab

如需进一步阅读,请参阅:C++17 引入的求值顺序保证是什么?

Since C++17 the behaviour of this code has changed; the left operand of << is sequenced before the right operand of <<, even when it is an overloaded operator. The output must now be ab.

For further reading see: What are the evaluation order guarantees introduced by C++17?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文