The solution is to build a char array (or a byte array in C ++ 17) and use a pointer from there to have an array of bars. To ensure proper alignment, a connection with one bar is sufficient:
#include <iostream>
As X builds in place, everything is acceptable for any C ++ 11 compiler, and you get a real random access container. There is no Undefined Behavior, because the memory is reserved by the char array, and the objects there are correctly constructed. This is a common trick to delay complex member initialization within the constructor with the full power of the language, when a thing becomes difficult to do with a simple initializer or with a compile-time metaprogram.
Here is my original idea, left here for reference, but really less pleasant ...
You can try to create a custom recursive array to allow the initialization of all its elements from a single value:
// X is the type of the "array", Y the type of its initializer, n the size template<class X, class Y=X, int n = 1> class RArr { public: X first; RArr<X, Y, n-1> other; RArr(Y& i) : first(i), other(i) {} X& operator [] (int i) { if (i >= n || i < 0) throw std::domain_error("Out of range"); if (i == 0) return first; return other[i - 1]; } }; // specialization for n=1 template <class X, class Y> class RArr<X, Y, 1> { public: X first; RArr(Y& i) : first(i) {} X& operator [] (int i) { if (i != 0) throw std::domain_error("Out of range"); return first; } }; struct Foo; struct Bar { Foo & foo; Bar(Foo & foo) : foo{ foo } {} }; //Cannot be default initialized struct Foo { RArr<Bar, Foo, BARS_IN_FOO> myBars; Foo() : myBars{ *this } //I want to initialize all Bars with Bar{*this} {} }; int main() { Foo foo; std::cout << &foo << std::endl; // shows that all foo.myBars[i] point to the original foo for (int i = 0; i < BARS_IN_FOO; i++) { std::cout << &(foo.myBars[i].foo) << std::endl; } return 0; }
Well, this works according to your requirements, but it is pretty useless, since all Bar elements reference the same Foo . This will only make sense when the Bar class contains other members.
Serge Ballesta
source share