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

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 16 19:02:36 PDT 2012


On Friday, August 17, 2012 03:52:38 Chris Cain wrote:
> On Friday, 17 August 2012 at 01:45:27 UTC, Mehrdad wrote:
> > He was clearly _not_ talking about modifying the pointer.
> > He said you cannot alter the "elements pointed TO".
> > 
> > 
> > Given that, I have no idea how that is supposed to be saying
> > "you can't modify the const _view_". He's clearly talking about
> > the target of the pointer, not the pointer itself.
> 
> I'll let Mr. Davis confirm which he was talking about. The only
> thing that's clear is that our understandings of his point differ.

If you have a const object, then you have the guarantee that none of what it 
contains or refers to either directly or indirectly can be altered through 
that reference or pointer. It's possible to alter it via other references, and 
without pure, const functions called on that object may still be able to alter 
by using global variables, but you do have the guarantee that that object and 
anything and everything that it refers to won't be directly mutated by that 
reference.

- Jonathan M Davis


More information about the Digitalmars-d mailing list