C++: input and output stream operators: associativity

陌路散爱 提交于 2019-12-04 12:43:11

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.

How this code sample correlates with Standard words about LEFT TO RIGHT execution?

The output from the print statement is 012, as required.

And why functions execution performed in RIGHT TO LEFT order?

Because that is completely up to the implementation. With a few exceptions, the standard says absolutely nothing about the order in which the arguments to an operator are computed. Those exceptions are the comma operator, the trinary operator a ? b : c, and the boolean short circuit operators && and ||. (These are not sequence points if the operators are overloaded). You should not depend on the order in which operands are computed. Associativity and the order in which the arguments are different concepts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!