I have an array called foos with instances of Foo. They are stored in an std.array and I would like to initialize them during compile time. Is that possible by using C++17 and constexpr?
struct Foo
{
constexpr void setA(int a);
int _a{0};
};
static std::array<Foo, 100> foos;
static constexpr void initialize()
{
int i = 0;
for (auto& e : foos)
{
e.setA(i++);
}
}
It seems the initialization is still done during runtime. Am I missing something?
https://gcc.godbolt.org/z/r4WUbE
I am aware that -O3 will generate better output, but my original example is slightly better and the compiler does not optimize it under this optimization.