Topic: operator, as a member


Author: smeyers@netcom.com (Scott Meyers)
Date: Sat, 29 Oct 1994 00:37:41 GMT
Raw View
Suppose one would like to write operator comma.  Futher suppose that one
would like to guarantee that the expression on the left will be
evaluated before the expression on the right.  It's clear that no such
guarantee can be made if operator, is a global function, but what if
operator, is a member function?

Consider this general form of a member function call:

  expression1.function(expression2);

Does the draft standard guarantee that expression1 must be fully
evaluated before expression2 is?  If so, it would seem that it's
possible to implement operator, as a member function and still get the
semantics I specified above.  On the other hand, if there is no such
guarantee, it's not.  For example:

  String operator+(const String&, const String&);
  String s1 = "This";
  String s2 = "That";

  class Widget {
  public:
    Widget(const String&);     // make a Widget from a String
    void operator,(const String&);
  };

  Widget("Hello"), s1 + s2;    // Widget("Hello").operator,(s1 + s2);

Are we guaranteed that the temporary Widget is constructed before
operator+ for Strings is called, or can a conforming compiler call
operator+ before calling the Widget constructor.  (Presumably it's clear
that the Widget constructor must be called before operator, can be.)

Scott