-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndexAtIndexTemplate.h
More file actions
63 lines (48 loc) · 2.11 KB
/
Copy pathArrayIndexAtIndexTemplate.h
File metadata and controls
63 lines (48 loc) · 2.11 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <array>
namespace ArrayIndexAtIndexTemplate {
template<size_t i, template<size_t> typename NumberConsumer>
struct Counter {
static void down() {
NumberConsumer<i>::apply();
Counter<i - 1, NumberConsumer>::down();
}
};
template<template<size_t> typename NumberConsumer>
struct Counter<0, NumberConsumer> {
static void down() {
NumberConsumer<0>::apply();
return;
}
};
template<typename ArrayType, ArrayType& arrayRef, template<size_t> typename ExternalNumberConsumer>
struct GenericArrayAssignment {
template<size_t i>
struct NumberConsumer {
static void apply() {
if (i == 0) return;
arrayRef[i - 1] = ExternalNumberConsumer<i - 1>::apply();
}
};
};
//Array = map({i < arraySize}, i->NumberConsumer(i))
template<typename ArrayEntryType, size_t arraySize, template<size_t> typename NumberConsumer>
struct FillArrayIndexDependent {
typedef std::array<ArrayEntryType, arraySize> ArrayType;
static ArrayType get() {
static_assert(arraySize >= 1, "arraySize has to at least 1!");
static_assert(!std::is_const<ArrayEntryType>(), "ArrayEntryType shall not be const!");
// In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.
Counter<arraySize, ConcreteArrayAssignment::NumberConsumer>::down();
return arr;
}
private:
static ArrayType arr;
typedef GenericArrayAssignment<ArrayType, arr, NumberConsumer> ConcreteArrayAssignment;
//C++11
//template<size_t m>
//using ConcreteArrayAssignmentNumberConsumer = typename ConcreteArrayAssignment::template NumberConsumer<m>;
};
template<typename ArrayEntryType, size_t arraySize, template<size_t> typename NumberConsumer>
typename FillArrayIndexDependent<ArrayEntryType, arraySize, NumberConsumer>::ArrayType FillArrayIndexDependent<ArrayEntryType, arraySize, NumberConsumer>::arr({});
}