The compiler now has a built-in function: narrowing conversions is not allowed when using list initialization.
You can write a traditional expression tester feature based on To { std::declval<From>() } and possibly add additional checks using std::is_integral and std::is_floating_point .
template <typename T> struct sfinae_true : std::true_type {}; struct can_cast_tester { template <typename From, typename To> sfinae_true<decltype(To { std::declval<From>() })> static test(int); template <typename...> std::false_type static test(...); }; template <typename From, typename To> struct can_cast // terrible name : decltype(can_cast_tester::test<From, To>(0)) {};
Theoretically, this should work, but currently it seems that neither GCC nor clang get it right.
source share