On Tuesday, 14 December 2021 at 13:25:04 UTC, apz28 wrote:
On Tuesday, 14 December 2021 at 05:04:46 UTC, Tejas wrote:
Is there anything wrong with the answer I posted?

Can you please tell me if there's anything dissatisfactory about it? I feel like it does everything the OP wants.

Also, am I wrong in using `Unconst` over `Unqual`? Isn't `Unqual` overkill if you just want to cast away `const`?

1. A template function should behave as to replace 4 overload functions
There is special logic for parameter type (2 vs 4...)

2. Your implementation does not remove 'const' as below
import std.traits : Unconst;
import std.stdio : writeln;

void foo2(T)(T x) if(is(Unconst!(T) : ulong)) {//You don't need Unqual for this
        pragma(msg, T.stringof);
        writeln(x);
    }

    void main()
    {
        import std.math;

        const int s1 = -3;
        int s2 = -2;
        foo2(abs(s1));
        foo2(abs(s2));

        enum byte b1 = 0;
        const byte b2;
        byte b3;
        foo2(b1);
        foo2(b2);
        foo2(b3);
    }
    --Output
    const(int)
    int
    byte
    const(byte)
    3
    2
    0
    0
    0

Then I suggest using `inout`, if you're trying to reduce number of template instantiations

```d
import std.traits : Unconst;
import std.stdio : writeln;

void foo2(T)(inout(T) x) if(is(Unconst!(T) : ulong)) {//You don't need Unqual for this
        pragma(msg, T.stringof);
        writeln(x);
}

void main(){
        import std.math;
        const int ci = -3;
        int i = -2;
        immutable int ii = 24342;
        foo2(abs(ci));
        foo2(abs(i));
        foo2(ii);
        
        byte b = 0;
        const byte cb;
        immutable byte ib;
        foo2(b);
        foo2(cb);
        foo2(ib);
        
        const long cl = 4554;
        long l = 12313;
        immutable long il = 3242343;
        foo2(cl);
        foo2(l);
        foo2(il);
        
}

Output(Compile-time):
int
byte
long

Output(Runtime):
3
2
24342

0
0
0

4554
12313
3242343
```


Reply via email to