References to const

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Nov 13 06:53:50 PST 2008


Steven Schveighoffer wrote:
> "Andrei Alexandrescu" wrote
>>> Is there a way to have variable reference to constant data in D?  I
>>> just don't see it at the moment and it would be _really_ useful.
>> http://www.digitalmars.com/d/2.0/phobos/std_typecons.html#Rebindable
> 
> Small question regarding Rebindable that I've been meaning to ask.
> 
> Does implicit assignment to a const(T) work, or does that require 
> opImplicitCast?  This would be useful not only for assignment, but also 
> passing to functions which take const(T) instead of Rebindable!(T).

I think we'd need compiler support for opImplicitCast indeed.

> Also, one other thing.  A Rebindable!(T) only makes sense if T is const or 
> invariant, but this is rather long winded:
> 
> Rebindable!(const(A)) a;
> 
> Would it make sense to have shortcuts to Rebindable that automatically apply 
> const or invariant?  something like:
> 
> template tconst(T)
> {
>    alias Rebindable!(const(T)) tconst;
> }
> 
> Using whatever you think is best for a symbol name.  Just something a little 
> shorter.

Better yet (or in addition to), we could make it a function so argument 
deduction takes care of everything.

Rebindable!(const T) tailconst(const T r);
Rebindable!(immutable T) tailimmutable(const T r);

auto w = new Widget;
auto w1 = tailconst(w);
w1 = tailconst(new Widget);


Andrei



More information about the Digitalmars-d mailing list