-
Notifications
You must be signed in to change notification settings - Fork 73
/
braced1.cxx
45 lines (36 loc) · 1.14 KB
/
braced1.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <vector>
#include <cstdio>
inline std::vector<int> find_primes(int count) {
std::vector<int> primes;
primes.reserve(count);
int cur = 2;
while(primes.size() < count) {
// Check if any element in primes divides cur.
bool divides = (... || (0 == cur % primes[:]));
// If cur is relatively prime against what has been computed, save it.
if(!divides)
primes.push_back(cur);
// Try the next elemnent.
++cur;
}
return primes;
}
// Compute the primes at compile time into an std::vector.
@meta std::vector<int> primes_vec = find_primes(47);
// Transfer them into a static array for runtime access.
// Expand
const int primes[] { primes_vec[:] ... };
// Print the primes at compile time. Any static data member is available
// at compile time as well as runtime, even though it was just created
// dynamically.
@meta for(size_t i = 0; i < std::size(primes); i += 10) {
@meta printf("%3d ", primes[i : i + 10])...;
@meta printf("\n");
}
int main() {
// Print the primes at runtime.
for(size_t i = 0; i < std::size(primes); i += 10) {
printf("%3d ", primes[i : i + 10])...;
printf("\n");
}
}