I am trying to initialize a multidimensional array, and while it's possible to populate this array once at startup, I really would prefer the array to be constexpr
, so I am wondering if there is a way to get the compiler to do this for me, particularly since I can provide a constexpr function that takes parameters for each index and returns the value the array should be at the index.
eg:
constexpr bool test_values[64][64][64][64] = {
... // magic goes here
};
And I have a function constexpr bool f(int,int,int,int)
that tells me what each element is supposed to be. I would prefer to access the entries via an array because it is faster to do an array lookup than it would be to call f() for non-const values.
Most of the other questions I've found relating to initializing an array at runtime used std::array rather than a C array, and none that I could find were multidimensional. I had tried unrolling the multidimensional array into a single dimensional one and using an std::array approach such as the what I found in one of the answers to this question, but I found that the resultant code produced by gcc 9.1 still populated the array once at startup, rather than the compiler generating the array directly.
Is there anything I can do to get the compiler to populate this kind of array, or am I stuck having to leave test_values
as effectively non-constexpr
, and initializing once at runtime?
EDIT:
for clarification, I am not intrinsically opposed to using an std::array instead of a builtin C-style array, but I do not think std::arrays are particularly friendly to multiple dimensions, and using a one-dimensional array obfuscates what my program needs to do (to be frank, I'll be willing to implement it as a one-dimensional std::array if I have to, but a multidimensional array feels less obfuscated than an equivalently sized one dimensional one that has been manually unwound, which is why I described it in terms of a multidimensional C array).