Skip to content

Instantly share code, notes, and snippets.

@MikimotoH
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikimotoH/a7cdfd31ff5ab70adeb6 to your computer and use it in GitHub Desktop.
Save MikimotoH/a7cdfd31ff5ab70adeb6 to your computer and use it in GitHub Desktop.
C++TMP to Test Prime Number
template<int N, int D>
struct IsPrimeND{
enum{
ret = (N%D != 0) && IsPrimeND<N, D - 1>::ret
};
};
// template specialization
template<int N>
struct IsPrimeND<N, 1>{
enum{
ret = true
};
};
template<int N>
struct IsPrimeND<N, 0>{
enum{
ret = true
};
};
template<int N>
struct IsPrime{
enum{
ret = IsPrimeND<N, N - 1>::ret
};
};
int main()
{
static_assert(IsPrime<1>::ret == true, "");
static_assert(IsPrime<2>::ret == true, "");
static_assert(IsPrime<6>::ret == false, "");
static_assert(IsPrime<30>::ret == false, "");
static_assert(IsPrime<37>::ret == true, "");
return 0;
}
@MikimotoH
Copy link
Author

clang -std=c++11 cxxtmp_prime_test.cxx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment