Topic: Non-evaluation of LHS of -> when calling static member function - why?


Author: "Crosbie Fitch" <crosbie@dircon.co.uk>
Date: 1999/06/11
Raw View
Has anyone ever changed the implementation of a member function and realised
it no longer needed the object and so made it static?

And then found that their program mysteriously failed?

class B
{ public:
    static int ComputeX();   // used to depend on instance of B
...
};

class List
{ public:
    B* Next();    // Desirable side-effect
} Fred;

Is there ever a sound reason why Fred.Next()->ComputeX(); shouldn't call
Next()?

Is this something that will be fixed in the standard or must programmers
forever avoid this gotcha?

Or has it already been fixed and I'm out of touch? Or am I mis-informed?

Three solutions:

1) Disallow anything other than explicit scope operator :: for accessing
static members, OR
2) Evaluate LHS of static member accesses, OR
3) Status quo - programmers that add 'static' should know what they're doing
anyway.

Related to this are quasi-functions like sizeof() not evaluating their
argument.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: wmm@fastdial.net
Date: 1999/06/12
Raw View
In article <7jqnnv$bio$1@soap.pipex.net>,
  "Crosbie Fitch" <crosbie@dircon.co.uk> wrote:

[example of calling static member function and thus losing desired
side effects in the object-expression]

> Is this something that will be fixed in the standard or must
programmers
> forever avoid this gotcha?
>
> Or has it already been fixed and I'm out of touch? Or am I
mis-informed?

It's been fixed (or broken, depending on your POV :-).  It used to
be the case that the object-expression was ignored when accessing
a static member.  Now the Standard says (9.4p2):

 A static member may be referred to using the class member
 access syntax, in which case the object-expression is
 always evaluated.

If your compiler still does not evaluate the object-expression in
accesses of static members, you should upgrade.

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]