What guarantees does D 'const' provide, compared to C++?
Chris Cain
clcain at uncg.edu
Thu Aug 16 18:25:16 PDT 2012
On Friday, 17 August 2012 at 01:17:19 UTC, Mehrdad wrote:
>
> How?
>
>
> Jon said "you know that the elements aren't altered either - or
> anything which the elements point to".
>
>
> I just showed that the const-ness of getStuff() tells you
> _nothing_ about that fact.
>
> Did I miss something?
Yeah. Again, you can't modify __the const view__. It's legal for
const things to be mutable underneath. And it's legal to modify
mutable things via mutable views. Ergo, const things can change
that way. But that's not what he's trying to tell you.
If you want something to be guaranteed to never change through
any means, you use immutable. And if you find a way to mutate
immutable things (without casts), it's a bug, not a feature (and
thus should be reported).
I'm pretty sure everyone's aware of the inconsistency with
immutable and the constructors at this time (like the bug I
pointed out above), so that's kind of a weak point where the
programmer actually has to know what they're doing, but I'm sure
that'll be fixed at some point.
More information about the Digitalmars-d
mailing list