Topic: Repost: Order of evaluation of implicit parameter?


Author: jason@cygnus.com (Jason Merrill)
Date: Fri, 20 Aug 1993 18:44:43 GMT
Raw View
/*
Summary:  Is a C++ compiler allowed to evaluate the explicit parameters to
a member function call before the implicit 'this' parameter?

It is not completely clear whether the semantics of object.fun(...) are any
different from those fun(object,...) for purposes of calculating the order
of evaluation of parameters, implicit or otherwise.  The ARM (May '91
print) doesn't seem to address the issue at all, at least not in section
5.2.2.

Is there anything in the draft standard which specifies that the implicit
parameter will be treated just like the others in determining the order of
evaluation?

On the i386-sequent-dynix platform, compiled with g++ 2.4.5, the following
program prints out "1 1 7" and then "1 1 2", indicating that the compiler
is treating them differently, but in the opposite way to the one you might
expect.  On the sparc-sun-sunos4.1.3 platform, it prints out "1 1 2" twice.
*/

#include <iostream.h>

class Thing
{
public:
  Thing() { saved = 0; }                                 // constructor
  Thing & save(int const & x) {saved = x; return *this;} // save argument
  Thing & retrieve(int & x)   {x = saved; return *this;} // modify argument
  friend Thing & save (Thing & t, int const & x) {t.saved = x; return t;}
  friend Thing & retrieve (Thing & t, int & x)   {x = t.saved; return t;}

private:
  int saved;
};

main()
{
  int x = 3, y = 4, z = 5;
  Thing t;

  t.save(1).retrieve(x).retrieve(y).save(x+y).retrieve(z);
  cout << x << " " << y << " " << z << endl;  // 1 1 7

  retrieve(save(retrieve(retrieve(save(t, 1), x), y), x+y), z);
  cout << x << " " << y << " " << z << endl;  // 1 1 2
}