 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ken Alverson Guest
|
Posted: Fri Jul 25, 2003 6:28 am Post subject: Re: parameter no_sideeffect (c++0x) |
|
|
"cody" <deutronium (AT) gmx (DOT) net> wrote
| Quote: |
and the operator + used in the example is written by your own, the
compiler have to call it twice. Perhaps there should be a parameter
'no_sideeffect' (it's name doesn't matters), that you use whle
overloading the operator. So the compiler can do something like this:
tmp = (foo + bar)
quer = tmp * tmp;
when you declare the operator as const as you can do with every method, the
compiler knows that the operator doesn't alter the object (and don't allow
your to do that) .
that should imho work.
|
const tells the compiler you are not modifying the object, but it doesn't tell
the compiler you aren't modifying global state, which could affect future
calls to the same function. What is needed is a way to let the compiler know
that if you call a function any number of times with the same exact inputs, it
will always have the same output. This *could* involve changing the global
state (for example, steps of the calculation might be memoized).
A smart enough compiler can sometimes figure this out, but such a compiler
could probably figure it out with or without the const modifier. However,
there are situations in which we can't reasonably expect the compiler to
figure it out, and it would be nice to allow this sort of optimization in
those situations. Example: what if it was a remote procedure call?
Ken
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Richard Smith Guest
|
Posted: Fri Jul 25, 2003 3:33 pm Post subject: Re: parameter no_sideeffect (c++0x) |
|
|
cody wrote:
| Quote: | when you declare the operator as const as you can do with every method, the
compiler knows that the operator doesn't alter the object (and don't allow
your to do that) .
that should imho work.
|
No. Making a method const does not grant the compiler
licence to remove duplicate calls to that function. It is
only allowed to do that it is knows that the function
*really* has no effects. For example, a const function
might still modify mutable data members, or it might change
the global state of the program, or if might print a
diagnostic message. Only if the compiler knows that none of
these apply can it remove the duplicate calls using the "as
if" rule.
--
Richard Smith
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|