#pragma once #include "tl/detail/prologue.h" #include "tl/plain_crash.h" #include "tl/type_traits.h" namespace tl { namespace detail { template struct is_same_signedness : integral_constant::value == is_signed::value> { }; } template T narrow(U u) noexcept { T t = static_cast(u); if (static_cast(t) != u) TL_PLAIN_CRASH("Narrowing error"); if (!detail::is_same_signedness::value && ((t < T{}) != (u < U{}))) TL_PLAIN_CRASH("Narrowing error"); return t; } }