ctPrimes

Construct an array of prime number using CTFE. The length of the array is length, and the type is T[length].

template ctPrimes (
size_t length
T = size_t
) if (
isIntegral!T &&
0 < length
) {
enum T[length] ctPrimes;
}

Parameters

length

Length of array

T

The type of the elements of the array

Examples

1 import std.random : uniform;
2 
3 auto runtimevalue = uniform(0, 5);
4 static assert(!__traits(compiles, {
5         ctPrimes!runtimevalue; // Error: variable runtimevalue cannot be read at compile time
6     }));
7 
8 static assert(__traits(compiles, {
9         auto a = ctPrimes!(5, byte);
10         auto b = ctPrimes!(5, ubyte);
11         auto c = ctPrimes!(5, short);
12         auto d = ctPrimes!(5, int);
13         auto e = ctPrimes!(5, long);
14     }));
15 
16 static assert(!__traits(compiles, {
17         auto a = ctPrimes!(5, bool);
18         auto b = ctPrimes!(5, char);
19         auto c = ctPrimes!(5, double);
20     }));
21 
22 auto primes1 = ctPrimes!(10);
23 static assert(is(typeof(primes1) == size_t[10]));
24 assert(primes1 == [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
25 
26 auto primes2 = ctPrimes!1;
27 assert(primes2 == [2]);

Meta