What’s the best C++ way to multiply unsigned integers modularly safely?
Some template metaprogramming with SFINAE, perhaps. #include <type_traits> template <typename T, typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) <= sizeof(unsigned int)) , int>::type = 0> T safe_multiply(T a, T b) { return (unsigned int)a * (unsigned int)b; } template <typename T, typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) > sizeof(unsigned int)) , int>::type = 0> T safe_multiply(T a, T b) { … Read more