I am looking for a way to fill an std::array<T, N>
with T being a non-default-constructible struct/class and N indeed being known at compile time, but so large, that you don't wanna hard-code all of them, such as N=256
. The constructor (in my case), takes one argument, which basically equals N
, and I wonder whether there is a way to get a constexpr std::array<T, 256> values
without going into a copy'n'paste hell.
There would be a workaround in using unique_ptr
and creating all elements upon construction of array<unique_ptr<T>, 256>
dynamically. But that would mean I cannot make use of constexpr
nor does it feel like the best solution.
I am providing a generalized(!) example of my problem, that I'd like to have working that way.
#include <iostream>
#include <array>
using namespace std;
struct T { int value; }; // some struct/class with non-default ctor
// a workaround-attempt
template <const size_t N> struct TT : public T { constexpr TT(): T{N} {} };
template <const size_t N>
constexpr array<T, N> makeArray() {
return {
TT<1>{},
TT<2>{},
TT<3>{},
TT<4>{},
TT<5>{},
// too bad I can't place this generically
};
}
ostream& operator<<(ostream& os, T const& t) { return os << t.value; }
int main() {
constexpr T a = TT<4>{};
cout << a << "\n";
constexpr array<T, 5> v = makeArray<5>();
for (T const& t: v) cout << " " << t.value;
cout << "\n";
return 0;
}
Here it should be clear, that I am currently hardcoding the makeArray<5>()
method by not using N
but explicitely returning an array of length 5 (in my code, that wouldn't be 5 but 256) :-).
I am currently coding in C++14 (no chance to upgrade to c++17 too soon), and I know that constexpr relaxations are improved in C++20, which isn't here either (unfortunately) :-)
How can I eliminate that and make the code look more clean in C++14?