Constness thrown away in D2.008

Walter Bright newshound1 at digitalmars.com
Thu Nov 29 12:12:11 PST 2007


Janice Caron wrote:
> Sadly, in D2.008, the constness of basic types seems to be "thrown
> away" when passed to templates. This means that code which used to
> work in D2.007 now doesn't work in D2.008, and for the life of me, I
> can't think of a workaround. Walter said that very few code changes
> were needed to get D2.007 const to work in D2.008. Well, I can't get
> this to work /at all/.
> 
> Here's the basic issue
> 
>     import std.stdio;
> 
>     template isConst(T)
>     {
>         static if (is(T == const)) static const bool isConst = true;
>         else static const bool isConst = false;
>     }
> 
>     void main()
>     {
>         writefln(isConst!(const(int)));
>     }
> 
> prints false. This used to work in D2.007 (with the minor modification
> that you'd write (T == const(T)) instead of (T == const). But the old
> way doesn't work either in D2.008.

This was done so that 'ordinary' templates don't get instantiated 
multiple times for int, const int, and invariant int when that is quite 
unnecessary for the vast bulk of templates.

> Just in case this is a bug, I've posted about this in digitalmars-d-bugs.

It's not a bug.

> But in case it's not, what code changes do I need to make, to make this work?

template isConst(T:T)



More information about the Digitalmars-d mailing list