2 つの数値型From
とが与えられますTo
。次のコードは、情報を失うことなくFrom
type の値を type の値として表すことができるかどうかを実際に判断しますか? To
はいの場合、より短い、またはより読みやすい決定方法はありますか?
template <class From, class To>
struct can_cast
{
static const bool value =
(std::numeric_limits<From>::is_integer || // either From is an integer type OR
std::is_floating_point<To>::value) && // ...they're both floating point types AND
(std::numeric_limits<From>::is_signed == false || // either From is unsigned OR
std::numeric_limits<To>::is_signed == true) && // ...they're both signed AND
(std::numeric_limits<From>::digits < std::numeric_limits<To>::digits || // To has more bits for digits than From OR
std::numeric_limits<From>::digits == std::numeric_limits<To>::digits && // To and From have same number of bits, but
std::numeric_limits<From>::is_signed == std::numeric_limits<To>::is_signed); // they're either both signed or both unsigned.
};