On 07/02/2016 09:23 AM, Johan Engelen wrote:
On Saturday, 2 July 2016 at 12:26:34 UTC, Andrei Alexandrescu wrote:

How would you reshape this?

Maybe:

U opCast(U, T)(T payload)
{
     import std.traits;

     enum descriptiveName = !isUnsigned!T && isUnsigned!U && T.sizeof <=
U.sizeof;
     enum unsT_sigU = isUnsigned!T && !isUnsigned!U;
     static if (unsT_sigU)
     {
         static assert(U.sizeof <= T.sizeof);
     }

     if (!descriptiveName || payload >= 0)
     {
         auto result = cast(U) payload;
         if (result == payload)
         {
             if (!unsT_sigU || result >= 0)
             {
                 return result;
             }
         }
     }

     return hook!U(payload);
}

Nice, thanks. I've tried to not rely too much on mixing statically-known and dynamically-known Boolean expressions. Can I safely assume that all compilers will generate good code for such? According to asm.dlang.org, dmd generates identical code at least for the two functions above. -- Andrei

Reply via email to