C ++:输入和输出流运算符:关联性 [英] C++: input and output stream operators: associativity

查看:166
本文介绍了C ++:输入和输出流运算符:关联性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入/输出流运算符的理论关联性:

从左向右

(例如,根据以下内容:说玛丽大学的网站

(for example, according to this: Sait Mary's University website

输入/输出流运算符在实践上的关联性:

#include <iostream>

int func0() {
  std::cout << "func0 executed" << std::endl;
  return 0;
}

int func1() {
  std::cout << "func1 executed" << std::endl;
  return 1;
}

int func2() {
  std::cout << "func2 executed" << std::endl;
  return 2;
}

int main() {
  std::cout << func0() << func1() << func2() << std::endl;
  return 0;
}

输出(MSVCPP 2010,2012):

Output (MSVCPP 2010, 2012):

func2 executed
func1 executed
func0 executed
012
Press any key to continue . . .

此示例演示了以RIGHT TO LEFT顺序调用函数(尽管按预期的LEFT TO RIGHT顺序打印了它们的值).

This sample demonstrates that functions are called in RIGHT TO LEFT order (despite of their values are printed as expected LEFT TO RIGHT).

问题: 此代码示例如何与有关LEFT TO RIGHT执行的标准单词相关联?为什么要按从右到右的顺序执行功能?

The question: How this code sample correlates with Standard words about LEFT TO RIGHT execution? And why functions execution performed in RIGHT TO LEFT order?

推荐答案

关联性定义了运算符的顺序.呼叫,将按以下方式依次发生:((((std::cout << func0()) << func1()) << func2()) << std::endl);.运算符<<<被评估的是实现定义的,但是iirc,这就是您要在此处进行的测试.

Associativity defines the order of the operator<< calls, which will happen in order this way: ((((std::cout << func0()) << func1()) << func2()) << std::endl);. The order in which the arguments to operator<< are evaluated is implementation-defined however, iirc, which is what you're testing here.

这篇关于C ++:输入和输出流运算符:关联性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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