Idiom for initializing an std::array using a generator function taking the index?
06:02 26 Jun 2024

Suppose I have a function T foo(size_t i). What would be an elegant and succinct way of constructing an object arr, of type std::array, so that we have arr[i] == foo(i)?

If possible, I would like for this construction to work even when T is not a default-initializable type.

Notes:

  • Since T is not default-initializable, the code can't start with std::array arr; and then some initialization.
  • The code must work for any value of N, generically.
c++ c++17 idioms stdarray object-construction