is it undefined behaviour?

From: Daniel Schüle (uval_at_rz.uni-karlsruhe.de)
Date: 03/30/05


Date: Wed, 30 Mar 2005 03:16:04 +0200

Hi all!

given the following code

#include <iostream>

using std::cout;
using std::endl;

struct X
{
     X & foo(int x){ cout << x << endl; return *this; }
};

void bad(int x, int y)
{
     cout << x << '\t' << y << endl;
}

int main()
{
  int a = 1, b = 2, c = 3; // 1
  cout.operator <<(a * b).operator <<(b * c).operator <<((a = 0, c = 0));

  a = 1, b = 2, c = 3;
  X x;
  x.foo(a).foo(b).foo(c); // 2

  a = 1, b = 2;
  bad(a * b, a=0); // 3

  return 0;
}

Is it safe to assume that 1 and 2 will produce on every C++
implementation the same output (no undefined behaviour here)?
Or may C++ Compiler compute all the values in () first (as it may do in
bad), and then invoke function foo?

thx in advance

-Daniel



Relevant Pages