I want to initialize my array items while avoiding unnecessary instances and copies (similar to this question: initialize std::array without copying/moving elements).
An initializer list does work for a small amount of objects.
Bit I want to do this via a code snippet since my array has several hundreds of items...
How can I do this?
#include <array>
#include <iostream>
class mytype {
public:
int a;
mytype() : a(0) {}
mytype(int a) : a(a) {}
};
int main() {
// explict constructor calls to instantiate objects does work
std::array<mytype, 2> a = { { mytype(10), mytype(20) } };
std::cout << a[0].a; // 10
// I want to do something like this - what does not work of course
std::array<mytype, 2> b = { { for (i = 0, i++, i < 2) mtype(10 * i); } };
}