What guarantees does D 'const' provide, compared to C++?

Chris Cain clcain at uncg.edu
Thu Aug 16 17:44:19 PDT 2012


On Friday, 17 August 2012 at 00:32:03 UTC, Mehrdad wrote:
> On Friday, 17 August 2012 at 00:10:52 UTC, Jonathan M Davis 
> wrote:
>> In contrast, in D,
>>
>> const ref Array!(T*) getStuff() const;
>>
>> you would _know_ that not only is the container not altered,
>> but you know that the elements aren't altered either -
>> or anything which the elements point to.
>
>
> I'm not so sure about that.
> ...snip...

Your example is not equivalent to what he was saying.

Also, D's const is _not_ a guarantee that there are no mutable 
references to something. That'd be immutable. It just says that 
it's illegal to modify something which is const (... i.e. 
_directly_ ... obviously, you can modify something which is const 
via a mutable reference dangling around, but that's _not_ 
something that const is supposed to protect you from)

>
> Right, it's the equivalent of static fields, see my example 
> above.

ditto to above


More information about the Digitalmars-d mailing list