Best practices for logical const
Jonathan M Davis
jmdavisProg at gmx.com
Sun Feb 16 00:26:13 PST 2014
On Sunday, February 16, 2014 06:55:36 Stanislav Blinov wrote:
> On Sunday, 16 February 2014 at 06:28:45 UTC, Jonathan M Davis
>
> wrote:
> > On Sunday, February 16, 2014 03:25:08 Stanislav Blinov wrote:
> >> On Saturday, 15 February 2014 at 04:03:51 UTC, Adam D. Ruppe
> >> wrote:
> >>
> >> What about a library solution for something like C++-esque
> >> mutable?
> >
> > You're casting away const if you do that, and that's precisely
> > what you shouldn't be doing. const is for _physical_ constness
> > and should never be used if you want logical constness...
>
> I hear you. In this case, one might ask why casting away const is
> allowed at all :)
Primarily for cases where you have to pass a const object to a function which
has mutable parameters, and you know that it's not going to mutate its
parameters - that and the fact that D is a systems language, so it will let
you blow off your foot if you really try. It _is_ possible to mutate a const
object without breaking code, but you have to know exactly what you're doing,
and it's very, very risky such that it's pretty much always a bad idea. But if
you really want to try blowing your foot off, D will let you. It just protects
you such that you can't do it without trying (e.g. by casting away const).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list