Why code failed to compile for foo2?
apz28
home at home.com
Tue Dec 14 13:25:04 UTC 2021
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
More information about the Digitalmars-d-learn
mailing list