You can use a variadics to generate an array
with elements initialized to an arbitrary function of the indices. Using the standard machinery for generating index sequences:
template <int... I> struct indices {};
template <int N, int... I> struct make_indices :
make_indices<N-1,N-1,I...> {};
template <int... I> struct make_indices<0,I...> : indices<I...> {};
it's fairly straightforward:
template <typename T, typename F, int... I>
inline std::array<T, sizeof...(I)> array_maker(F&& f, indices<I...>) {
return std::array<T, sizeof...(I)>{ std::forward<F>(f)(I)... };
}
template <typename T, std::size_t N, typename F>
inline std::array<T, N> array_maker(F&& f) {
return array_maker<T>(std::forward<F>(f), make_indices<N>());
}
Which lets us do anything from duplicating the effect of std::iota
:
auto a = array_maker<int,10>([](int i){return i;});
to making an array with the squares of the first 10 natural numbers in reverse order:
const auto a = array_maker<std::string,10>([](int i){
return std::to_string((10 - i) * (10 - i));
});
Since your Node
is movable, this allows you to define your ParentNode
constructor as:
ParentNode()
: _children(array_maker<Node, count>([](unsigned i){return i+1;}))
{}