Meta:

Wouldn't it be better to do this at the call site anyway? Just use a simple function.

Unqual!T unconst(T)(T val)
if (isScalarType!T)
{
    return val;
}

void foo(T)(T a, T b)
{
    a = b;
    b = a;
}

void main()
{
    const int x, y;
    foo(x.unconst, y.unconst);
}

That's an option. Currently Phobos doesn't use that strategy, but adds a recursive call, like in std.numeric.gcd:


T gcd(T)(T a, T b) {
    static if (is(T == const) || is(T == immutable)) {
        return gcd!(Unqual!T)(a, b);
    } else {
        static if (T.min < 0) {
            //enforce(a >= 0 && b >=0);
            assert(a >= 0 && b >=0);
        } while (b) {
            auto t = b;
            b = a % b;
            a = t;
        }
        return a;
    }
}


I don't know what's better, but regular functions are able to "strip away" the const/immutable of their input values, so perhaps we should have a mean to do the same with template functions (and not at the call site) without the need of recursive calls like that one and without creating mutable local values like this:

Unqual!T mutableA = a, mutableB = b;

Bye,
bearophile

Reply via email to