Compile-time sieve of Eratosthenes There are many instances of prime number sieve implementation both here and other places on the web, but I wanted something a little different.  In particular, I wanted to create a static array of the first 1024 prime numbers at compile time as a simple reusable structure.  I also wanted to allow for relatively simple creation of larger or smaller arrays.  

I combined the code from this question with a little bit of macro magic from this other question to come up with code that meets my goal.

Questions:

I'd like to be able to use something more elegant than the stack of macros (templates perhaps), and would prefer to have the result be something more like an STL container than a raw array.  Comments on how to do those things, or comments generally to clean up the code would be welcome.

primeconsttest.cpp

#include "primeconst.h"    
#include &lt;vector&gt;
#include &lt;iterator&gt;
#include &lt;iostream&gt;

int main() {
    std::vector&lt;int&gt; prime{std::begin(primes), std::end(primes)};
    for (unsigned i=0; i &lt; prime.size(); ++i)
        std::cout &lt;&lt; i &lt;&lt; '\t' &lt;&lt; prime[i] &lt;&lt; '\n';
}


primeconst.h

extern const int primes[1024];


primeconst.cpp

constexpr bool isPrimeLoop(int i, int k) {
    return (k*k &gt; i)?true:(i%k == 0)?false:isPrimeLoop(i, k + 1);
}

constexpr bool isPrime(int i) {
    return isPrimeLoop(i, 2);
}

constexpr int nextPrime(int k) {
    return isPrime(k)?k:nextPrime(k + 1);
}

constexpr int getPrimeLoop(int i, int k) {
    return (i == 0)?k:
        (i % 2)?getPrimeLoop(i-1, nextPrime(k + 1)):
        getPrimeLoop(i/2, getPrimeLoop(i/2, k));
}

constexpr int getPrime(int i) {
    return getPrimeLoop(i, 2);
}

static_assert(getPrime(511) == 3671, "computed incorrectly");

#define K(x) J(x) J(x + 512)
#define J(x) I(x) I(x + 256)
#define I(x) H(x) H(x + 128)
#define H(x) G(x) G(x +  64)
#define G(x) F(x) F(x +  32)
#define F(x) E(x) E(x +  16)
#define E(x) D(x) D(x +   8)
#define D(x) C(x) C(x +   4)
#define C(x) B(x) B(x +   2)
#define B(x) A(x) A(x +   1)
#define A(x) getPrime(x) ,

extern constexpr int primes[] = { K(0) };


Output

0   2
1   3
2   5
3   7
4   11
5   13
6   17


...

1017    8093
1018    8101
1019    8111
1020    8117
1021    8123
1022    8147
1023    8161
I believe I can help with these things:


Obliterate macros
Obliterate raw arrays


Macros can be replaced with std::integer_sequence (C++14), and raw arrays can be replaced with std::array (C++11).

#include &lt;array&gt;
#include &lt;utility&gt;

template &lt;typename T, T... Is&gt;
constexpr auto gen_primes_helper(std::integer_sequence&lt;T, Is...&gt;) {
    return std::array&lt;T, sizeof...(Is)&gt;{{getPrime(Is)...}};
}

// T: integer type
// N: number of elements
// return: std::array&lt;T,N&gt;
template &lt;typename T, T N&gt;
constexpr auto gen_primes() {
    return gen_primes_helper(std::make_integer_sequence&lt;T,N&gt;());
}

constexpr auto primes = gen_primes&lt;int,1024&gt;();


However, we have a problem: Compilers currently implement std::make_integer_sequence in O(N) complexity.

GCC limits N to 900, unless you add the -ftemplate-depth= compiler option. See this bug.

Clang limits N to a pathetic 256, but also provides the same -ftemplate-depth= option.

To solve the problem, std::make_integer_sequence can be implemented in O(log(N)). I have done this as follows:

#include &lt;array&gt;
#include &lt;utility&gt;

namespace logseq {

// alias for std::integer_sequence, for brevity
template &lt;typename T, T... Is&gt;
using intseq_t = std::integer_sequence&lt;T, Is...&gt;;

template &lt;typename A, typename B&gt;
struct concat;

// A: intseq_t
// B: intseq_t
// return: typename intseq_t
//
// Example:
//   concat_t&lt; intseq_t&lt;int,0,1,2&gt;, intseq_t&lt;int,0,1,2,3&gt; &gt;
//     =&gt; intseq_t&lt;int,0,1,2,3,4,5,6&gt;
template &lt;typename A, typename B&gt;
using concat_t = typename concat&lt;A,B&gt;::type;

template &lt;typename T, T... As, T... Bs&gt;
struct concat&lt;intseq_t&lt;T, As...&gt;, intseq_t&lt;T, Bs...&gt;&gt; {
    using type = intseq_t&lt;T, As..., (sizeof...(As) + Bs)...&gt;;
};

template &lt;typename T, T N, typename = void&gt;
struct logseq;

// T: integer type
// N: number of elements
// return: typename std::make_integer_sequence&lt;T,N&gt;
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66059
template &lt;typename T, T N&gt;
using logseq_t = typename logseq&lt;T,N&gt;::type;

template &lt;typename T, T N, typename&gt;
struct logseq {
    using type = concat_t&lt;logseq_t&lt;T,N/2&gt;,logseq_t&lt;T,N-N/2&gt;&gt;;
};

template &lt;typename T, T N&gt;
struct logseq&lt;T,N,typename std::enable_if&lt;N==0&gt;::type&gt; {
    using type = intseq_t&lt;T&gt;;
};

template &lt;typename T, T N&gt;
struct logseq&lt;T,N,typename std::enable_if&lt;N==1&gt;::type&gt; {
    using type = intseq_t&lt;int,0&gt;;
};

} // namespace logseq


template &lt;typename T, T... Is&gt;
constexpr auto gen_primes_helper(std::integer_sequence&lt;T, Is...&gt;) {
    return std::array&lt;T, sizeof...(Is)&gt;{{getPrime(Is)...}};
}

// T: integer type
// N: number of elements
// return: std::array&lt;T,N&gt;
template &lt;typename T, T N&gt;
constexpr auto gen_primes() {
    using logseq::logseq_t;
    return gen_primes_helper(logseq_t&lt;T,N&gt;());
}

constexpr auto primes = gen_primes&lt;int,1024&gt;();


Namespace logseq provides a metafunction logseq_t that returns a typename std::integer_sequence&lt;T, 0, /* ... */, N-1&gt;. It is a drop-in replacement for std::make_integer_sequence, which returns the same type.

logseq_t allows for infinite (read: really freaking big) values of N.