(this T) versus (const K), TransferConst!, etc.

Christopher Wright dhasenan at gmail.com
Sun Mar 23 16:24:33 PDT 2008



Janice Caron wrote:
> The problem is, I don't grok how to return a type whose constancy
> depends on the constancy of this, but isn't actually T.

template ThisConst(TConst, TBase)
{
    static if (is (TConst == const))
    {
       alias const(TBase) ThisConst;
    }
    else static if (is (TConst == invariant))
    {
       alias invariant(TBase) ThisConst;
    }
    else
    {
       alias TBase ThisConst;
    }
}

class C
{
    ThisConst!(T) foo(this T)() { ... }
}

Completely untested, since I don't use dmd2 anymore.

> But we'd also have an added bonus. Because we have K (a placeholder
> for one of the keywords "auto", "const" or "invariant") readily to
> hand, we can do all sorts of other cool stuff with it, without all
> that mucking about with TransferConst!(). Even ignoring the fact that
> TransferConst! is poorly documented, (const K) gives you readily
> comprehensible code.
> 
> Any chance we could ditch (this T) and TransferConst!() in favor of (const K)?

That is certainly a cleaner solution.



More information about the Digitalmars-d mailing list