On 03/25/2017 10:41 AM, Alvin wrote:
> Is there a way to create a constexpr array in C++14, without using
> variadic templates or template recursion? I want to achieve the following:
No, probably not, but here's how to do it that way anyway if anyone's
interested:
https://ideone.com/K8eAsP
(Sourced and corrected from here:
<
http://cplusadd.blogspot.com/2013/02/c11-compile-time-lookup-tablearray-with.html>)
> #include <array>
>
> constexpr size_t array_size = 1000000;
>
> constexpr auto makeArray() {
> std::array<float, array_size> a = {};
> for(int i = 0; i < a.size(); i++)
> a[i] = 0.1f * i;
> return a;
> }
>
> constexpr static auto a = makeArray();
>
>
>
> This works with C++17, which made a lot of array members constexpr, but
> doesn't work with C++14.
>
> I know how to make it work using std::index_sequence or template
> recursion, but that doesn't really work for large sizes.
In GCC at least you can increase the template recursion depth (up to
some limit I don't know what, probably depending on hardware) with the
"-ftemplate-depth-X" flag.
As Alf P. says for truly gigantic constant lookup tables it probably
doesn't make sense to use C++ itself to generate them.