It might need polishing but that's what I thought:
template <std::size_t... ns, typename Fn>
auto fill_helper(std::integer_sequence<std::size_t, ns...>, Fn&& fn) -> std::array<decltype(fn(std::size_t())), sizeof...(ns)> {
return {{fn(ns)...}};
}
template <std::size_t N, typename Fn>
auto fill(Fn&& fn) {
using seq = std::make_integer_sequence<std::size_t, N>;
return fill_helper(seq(), std::forward<Fn>(fn));
}
int main() {
auto plus5 = [](std::size_t index) {
return index + 5;
};
auto const as = fill<5>(plus5);
for (auto&& a: as)
std::cout << a;
}
The advantage is that you are initializing the array with the proper values, rather than first initializing and then assigning.