3

I want to fill a constexpr std::array in compile time using a math function. Is it possible in an easy way?

I found this solution: C++11: Compile Time Calculation of Array. However, is there any other modern solution using only std? This one seems too confusing for me.

int main()
{
   // This is the array I need to fill
   constexpr std::array<double, 100000> elements;
   for (int i=0; i!=100000; ++i)
   {
      // Each element is calculated using its position with long exponential maths.
      elements[i] = complexFormula(i); // complexFormula is constexpr
   }


   double anyVal = elements[43621];
   // ...
}


2 Answers2

6

Here's a non-confusing approach: wrap the calculation in a function:

template <int N>
constexpr std::array<double, N> generate()
{
    std::array<double, N> arr{};
    for (int i = 0; i < N; ++i)
        arr[i] = complexFormula(i);
    return arr;
}

Usage example:

constexpr std::array<double, 10000> arr = generate<10000>();

(live demo)

This works because, since C++14, loops are allowed in a constexpr function, and variables can be modified as long as their lifetime starts within the evaluation of the constant expression.

L. F.
  • 19,445
  • 8
  • 48
  • 82
0

In c++14 you also have the possibility of using std::index_sequence for your purpose:

template <std::size_t... I>
constexpr std::array<double, sizeof...(I)> generate_impl(std::index_sequence<I..>) noexcept
{
   return { complexFormula(I)... };
}

template <std::size_t N>
constexpr std::array<double, N> generate() noexcept
{
   return generate_impl(std::make_index_sequence<N>{});
}
francesco
  • 7,189
  • 7
  • 22
  • 49