Two cases showing imperfection of the const system

Jonathan M Davis jmdavisProg at
Thu Feb 16 21:59:30 PST 2012

On Friday, February 17, 2012 06:54:44 SiegeLord wrote:
> On Friday, 17 February 2012 at 02:39:29 UTC, Andrei Alexandrescu
> wrote:
> > That's a given. But that doesn't confer them infinite powers
> > otherwise inaccessible; you seem to require any flexibility
> > that seems reasonable within a context, and that's simply put
> > impossible. There is a point where inout's powers stop (inout
> > can be considered a special case designed for a few common
> > cases).
> And can this not be a special case with a new type too? The issue
> is of moving the elements in an array while retaining the const
> correctness on the contents of the elements. Something like a
> rebindable reference to a constant memory?

If the elements are really const, then you can't move them. Period. Doing so 
would violate const. Now, you can have references or pointers to const and 
move _those_ around, but then the elements themselves aren't really const. If 
you're dealing with objects, you can use std.typecons.Rebindable, though there 
is a pull request (which has been around a while and may never get merged in) 
which adds syntax for references to const, in which case Rebindable wouldn't 
be needed anymore.

> > This calls for the obvious answer that there's a subset of D
> > that's usable without const.
> Yes, but it doesn't have these bizzarities.
> I like having const correctness in my code, I like the assurance
> that my memory isn't being mutated. Once I start using templates,
> it seems that that assurance can go out of the window sometimes
> because the template system is defined on the level of types, not
> on the level of const qualifiers.

Templates can't violate const anymore than other code can. Templates don't 
strip constness. If you're passing const variables to a templated function, 
they're const in the templated function. And if the templated function wants 
to guarantee that what you pass in doesn't get altered, then it can mark its 
parameters const just like any other function can. Templates do nothing to 
screw up const.

- Jonathan M Davis

More information about the Digitalmars-d mailing list