constancy and template parameters in D2.009+

Janice Caron caron800 at googlemail.com
Thu Dec 13 11:41:40 PST 2007


On 12/13/07, Janice Caron <caron800 at googlemail.com> wrote:
> On 12/13/07, Christopher Wright <dhasenan at gmail.com> wrote:
> > Of course, if you say that the constness of parameters should always be
> > preserved, you'd get:
> >
> > template RemoveHeadConst(T) {
> >     static if (is (T : const(U), U)) {
> >        alias U RemoveHeadConst;
> >     } else {
> >        alias T RemoveHeadConst;
> >     }
> > }
>
> Assuming that works, yes. It doesn't in D2.008, and I don't believe it
> did in D2.007 (although you'd imagine it should).

No wait - I must correct myself - I posted without thinking it through.

That template will remove ALL const, not just head const. You need
something that transforms const(int[]) into const(int)[]. And it has
to work for invariant too. (And even for const pointer to invariant -
yes such things /are/ allowed to exist in D).

It's a much harder template to write than you might first assume.

...which is why I like D2.008's default, in every regard but the
syntax (and the fact that it has some bugs which mean it doesn't
actually work as advertised in all cases).



More information about the Digitalmars-d mailing list