Boost logo

Boost :

From: Stephen Nutt (snutt_at_[hidden])
Date: 2003-09-16 20:26:12


Is there any interest in a set of template that generate/validate large
prime number at compile time? I'm using MSVC 6.0 and can validate numbers
in the low-mid hundreds of millions, and generate them up to around 100
million. Eventually I get compiler limit reached errors!

I've run across templates in the past that have required prime numbers to be
specified as a template argument, for example a fixed hash table size.

Now the primality of the number can be checked at compile time

template <typename T, unsigned tableSize> class hash_table
{
    BOOST_STATIC_ASSERT (is_prime<tableSize>::value);

    T table[tableSize];
...
};

or if the table size is a hint

template <unsigned tableSizeHint> class hash_table
{
    BOOST_STATIC_CONSTANT (unsigned, tableSize =
greatest_prime_less_or_equal<tableSizeHint>::value);
    BOOST_STATIC_ASSERT (is_prime<tableSize>::value);

    T table[tableSize];
...
};

To accomplish this I also had to write the two following templates

template <unsigned x, unsigned y> struct power;
template <unsigned x, unsigned y> struct root;

where power calculates x^y and root generates the y'th root of x. Both are
integer based and I believe will handle all legal positive integer values.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk