C++ noob here. What's the simplest way to create the array {f(0), f(1), ..., f(1023)}
at compile time, given a constexpr f
?
You can use an immediately invoked lambda:
#include <array>
using ResultT = int;
constexpr ResultT f(int i)
{
return i * 2;
}
constexpr auto LUT = []
{
constexpr auto LUT_Size = 1024;
std::array<ResultT, LUT_Size> arr = {};
for (int i = 0; i < LUT_Size; ++i)
{
arr[i] = f(i);
}
return arr;
}();
static_assert(LUT[100] == 200);