// (by Ariel Badichi) #include <boost/mpl/eval_if.hpp> #include <boost/mpl/less.hpp> #include <boost/mpl/and.hpp> #include <boost/mpl/equal_to.hpp> #include <boost/mpl/modulus.hpp> #include <boost/mpl/int.hpp> #include <boost/mpl/next.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/not.hpp> #include <boost/mpl/print.hpp>
namespace mpl = boost::mpl;
template<typename N, typename I> struct check : mpl::eval_if< mpl::less<I, N>, mpl::and_< mpl::not_< mpl::equal_to< mpl::modulus<N, I>, mpl::int_<0> > >, check<N, typename mpl::next<I>::type> >, mpl::true_ > {};
template<typename N> struct is_prime : check<N, mpl::int_<2> > { BOOST_MPL_ASSERT_RELATION(N::value, >=, 1); };
BOOST_MPL_ASSERT((is_prime<mpl::int_<1> >)); BOOST_MPL_ASSERT((is_prime<mpl::int_<2> >)); BOOST_MPL_ASSERT((is_prime<mpl::int_<3> >)); BOOST_MPL_ASSERT((is_prime<mpl::int_<5> >)); BOOST_MPL_ASSERT((is_prime<mpl::int_<7> >));
template<typename I, bool> struct disp;
template<typename I> struct disp<I, false> : mpl::false_ {};
template<typename I> struct disp<I, true> : mpl::false_ { mpl::print<mpl::int_<I::value> > x; };
template<typename I, typename N> struct gen : mpl::and_< mpl::less<I, N>, gen<typename mpl::next<I>::type, N> > { disp<I, is_prime<I>::value> huhu; };
template<int N> struct gen_primes : gen<mpl::int_<1>, mpl::int_<N> > {};
int main() { typedef gen_primes<10>::type x; }
// by Pablo Aguilar #include <boost/mpl/print.hpp> #include <boost/mpl/if.hpp>
template<int N, int D> struct divides { // Loop until we reach 1, or the largest number D that divides N enum { value = ((N % D) == 0) ? true : divides<N, D - 1>::value }; };
template<int N> struct divides<N,1> { enum { value = false }; };
template<int N> struct is_prime { enum { value = !divides<N,N - 1>::value }; };
template<int N> struct show_primes : show_primes<N - 1> { typename boost::mpl::if_c< is_prime<N>::value , boost::mpl::print<is_prime<N> > , int >::type m_print; };
// Terminate loop template<> struct show_primes<1> { enum { value = false }; };
int main() { int x = show_primes<100>::value; std::cin.get(); return 0; }