3

Input/Output stream operators associativity in theory:

LEFT TO RIGHT

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

Input/Output stream operators associativity on practice:

#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;
}

Output (MSVCPP 2010, 2012):

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

This sample demonstrates that functions are called in RIGHT TO LEFT order (despite of their values are printed as expected 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?

nickolay
  • 3,643
  • 3
  • 32
  • 40

2 Answers2

3

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.

jsinger
  • 807
  • 5
  • 7
  • jsinger, thank you for your answer. So you mean that associativity and arguments evaluation order are different things and while first one is standard defined, last - in all above on the particular compiler implementation. Am I correct? – nickolay Jul 06 '12 at 02:09
  • 2 jsinger, David. Guys, the both answers are great. It was hard to select one where to place the green mark. I decided to mark the first one that appeared. – nickolay Jul 06 '12 at 02:28
1

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.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • thank you. Analogy - function argument list calculation order. – nickolay Jul 06 '12 at 02:23
  • I think the comma operator comment is a bit of a red herring as it is not used here and in practice hardly ever used (outside a for statement). Note comma to separate parameters is **NOT** the comma operator. – Martin York Jul 06 '12 at 02:35
  • 1
    "The one exception is the comma operator." And the logical-OR operator. And the logical-AND operator. – Ben Voigt Jul 06 '12 at 02:36
  • @BenVoigt So there are several exceptions :) – nickolay Jul 06 '12 at 05:21
  • @BenVoigt: You're correct; well almost. You forgot the trinary operator. I corrected my answer. To compound matters, these operators do not form a sequence point if overloaded. – David Hammen Jul 06 '12 at 12:12