Compile time prime checking
问题 I need to check is some integer prime in compile time (to put the boolean value as template argument). I've write code that do it well: #include <type_traits> namespace impl { template <int n, long long i> struct PrimeChecker { typedef typename std::conditional< (i * i > n), std::true_type, typename std::conditional< n % i == 0, std::false_type, typename PrimeChecker<n, (i * i > n ) ? -1 : i + 1>::type >::type >::type type; }; template <int n> struct PrimeChecker<n, -1> { typedef void type; }