const confusion

Jarrett Billingsley jarrett.billingsley at gmail.com
Mon Jun 1 13:36:39 PDT 2009


On Mon, Jun 1, 2009 at 4:01 PM, Witold Baryluk <baryluk at smp.if.uj.edu.pl> wrote:
> Dnia 2009-05-31, nie o godzinie 15:36 -0400, Jarrett Billingsley pisze:
>> On Sun, May 31, 2009 at 3:26 PM, Witold Baryluk
>> <baryluk at smp.if.uj.edu.pl> wrote:
>> >
>> > Horrible.
>> >
>> > How to ensure constness of data, and still have possibility of changing references of local variables?
>>
>> Rebindable.
>>
>> http://www.digitalmars.com/d/2.0/phobos/std_typecons.html#Rebindable
>
> Thanks. I was already thinking about implementing something like this.
> It is very thin, and probably doesn't eat even single byte more than
> original reference.
>
> So generally we need to cheat: union with const and non-const version +
> opAssign/opDot, and some hidden casts. If everybody is doing this, why
> not.
>
> Only one problem is that i need to make some wrappers for it:
>
> alias Rebindable!(C) CC;
>
>
> first try:
>
> auto c1 = CC(new C(1));
> auto c2 = CC(new C(2, c1)); // oops doesn't work
> c2 = c2.b();
>
> second try:
>
> auto c1 = CC(new C(1));
> auto c2 = CC(new C(2, c1.opDot())); // ok, works
> c2 = c2.b();
>
>
> define some function on original data:
>
> int something(in C c) {
>        return c.a;
> }
>
> something(c2); // oops, doesn't work
>
> something(c2.opDot()); // ok, works
>
>
>
> So generally now i need to overload all my functions to support also
> Rebindable!(C), where I will unwrap object and call original function?
> The same with constructors.
>
> Can't be this done more simpler?
>
> As I remember there was something like opCast (for explicit casts)?
> Maybe Rebindable should have it casting to original type (with const)?

This seems like a perfect application for opImplicitCast, a feature
that has been bandied about for years and which Andrei seems to have
hinted at coming soon.  Using implicit casts, you would be able to
make a perfectly transparent wrapper type such as Rebindable.

For now, you're stuck overloading on Rebindable!(T) :\


More information about the Digitalmars-d-learn mailing list