I don't know a solution for C-style arrays, though with constexpr and C++17 you could do this with std::array.
constexpr std::array<int, SIZE> createFilledArray (int value){ std::array<int, SIZE> a{0}; for (auto i = 0; i < SIZE; ++i) a[i] = value; return a; } static constexpr auto myArr = createFilledArray(42);
Code at compiler explorer
The disadvantage of this is that you can't change the array. If you remove the constexpr from the variable, your compiler should be able to optimize this.
From C++20 on, you can force the initialization:
static constinit auto myArr = createFilledArray(42);
Not sure if the proposal is already merged in: see constinit proposal
#define V 42#define INIT4 V, V, V, V#define INIT6 INIT4 , V, Vand then placing#define INIT <XXX>in those#ifelsestoo dumb? Withstatic int myArr[] = { INIT};It's not so pretty, but it get's the job done.std::arrayan option?