std.digest can't CTFE?

Stefan Koch uplink.coder at googlemail.com
Fri Jun 1 14:56:32 UTC 2018


On Thursday, 31 May 2018 at 21:29:13 UTC, Manu wrote:
> "CTFE
> Digests do not work in CTFE"
>
>
> That's an unfortunate limitation... why is, those things? :(

Because CTFE cannot do things which are technically ABI dependent.
You can work around it with code like this:

T fromBytes(T, Endianess endianess = Endianess.LittleEndian) 
(const ubyte[] _data)
pure {
     static assert(is(T : long)); // poor man's isIntegral
     T result;
     static if (endianess == Endianess.LittleEndian) {
         static if (T.sizeof == 4) {
             result = (
                 _data[0] |
                 (_data[1] << 8) |
                 (_data[2] << 16) |
                 (_data[3] << 24)
             );
         } else static if (T.sizeof == 8) {
             result = (
                 _data[0] |
                 (_data[1] << 8) |
                 (_data[2] << 16) |
                 (_data[3] << 24) |
                 (cast(ulong)_data[4] << 32UL) |
                 (cast(ulong)_data[5] << 40UL) |
                 (cast(ulong)_data[6] << 48UL) |
                 (cast(ulong)_data[7] << 56UL)
             );
         } else {
             static assert(0, "only int and long are supported");
         }
     } else {
         static assert(0, "Big Endian currently not supported");
     }

     return result;

}



More information about the Digitalmars-d mailing list