Topic: Question about another C++ optimization


Author: shankar@engr.sgi.com (Shankar Unni)
Date: 1995/12/02
Raw View
Consider the code:

  void foo(const int &);

  int x;
  ...
  foo(x);

Can an optimizer assume that x is not going to be modified, and keep it in
a register across the call?  Or can it convert the parameter (if foo is
inlined) into a value parameter (for ints, this is usually a win)?

We've been trying to read the language in the draft WP (possibly a little
too deeply).

It seems to imply that if you cast a const pointer or reference to a
non-const type and dereference it to assign to it, it is legal if the
original object it is pointing to was non-const..

This would basically make the "const" information absolutely useless to the
caller, and act only as a sort of rap on the knuckles if you actually try
to assign it *directly*..

Is this really the intent of the standard? That a compiler cannot assume in
the above code that x is not modified?
--
Shankar Unni    E-Mail: shankar@sgi.com
Silicon Graphics Inc.   Phone: +1-415-933-2072
URL: http://reality.sgi.com/employees/shankar



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]