Re: [C++] Need help with program please
From: Alwyn (alwyn_at_blueyonder.co.uk)
Date: Mon, 31 Jan 2005 13:18:05 +0000
On Sun, 30 Jan 2005 22:32:43 -0800, James Dennett wrote:
> It's a bit of a mess, defining which operations are allowed
> and which are not, I'll openly admit. But not all effects
> are "side effects"; something done by a function whose only
> effect is to determine the return value isn't a side-effect.
> Effects other than those *are* side-effects. But we probably
> don't have language in the standard to formalize that.
The term 'side effect' is defined as an alteration is the state of the
system. Assignment to global variables and I/O are usually quoted as
typical examples. However, there's a point of view that says that _any_
destructive update is a side effect, even if it is to an automatically
allocated local variable. So:
i = 5;
is side-effecting, while:
const int i = 5;
is not, though the net effect of both is often the same, with or without
the 'const' keyword in the second example.
Personally, I can accept the current 'no side effects' rule for
'std::transform', though, from a practical point of view, it can appear
unduly restrictive, as long as it is made clear that 'std::for_each' can
modify sequence values through the iterators passed to it. This latter
seems to me to be the real anomaly.
Another anomaly would be that absence of side effects is required by the
standard without there being any means in the language to enforce such a
condition. You could introduce a keyword into the language for this
purpose, but even then, the compiler would not be able to verify in all
cases whether the programmer has kept his or her promise. Basically, this
is a mess which only pure functional languages are able to solve (perhaps
at some cost).