On Sunday, 19 August 2012 at 19:58:11 UTC, Era Scarecrow wrote:
On Sunday, 19 August 2012 at 19:42:20 UTC, Peter Alexander
wrote:
On Sunday, 19 August 2012 at 19:26:58 UTC, Jesse Phillips
wrote:
While in context with the original question this is fine, but
I do not like this use of guarantee.
What I mean is, const does provide guarantees by itself. And
it provides more than C++ because it is transitive and
modifying a const reference is undefined.
What guarantees does const provide on its own?
If you don't circumvent the language by casting/forcing it,
then const (and immutable) items cannot be changed by the
functions called with them, nor them nor anything they contain
or reference.
class Foo
{
static Foo sneaky;
this() { sneaky = this; }
void bar() const { sneaky.x++; }
int x = 0;
}
const(Foo) f = new Foo();
assert(f.x == 0);
f.bar();
assert(f.x == 1);
You only have that guarantee if there are no other mutable
references to the data. const *on its own* does not provide that
guarantee.