Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

constexpr array

9 views
Skip to first unread message

Alvin

unread,
Mar 25, 2017, 10:42:00 AM3/25/17
to
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:



#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.

I could easily make it work with my own array class, but I don't see how
I can efficiently convert it into a constexpr std::array.

Alf P. Steinbach

unread,
Mar 25, 2017, 10:56:08 AM3/25/17
to
One easy way to generate code or compile time data without cajoling the
C++ compiler or preprocessor into doing it, is to use a script (e.g.
Python, or node.js, whatever) to generate that code.

Easy peasy. ;-)


Cheers!,

- Alf

bitrex

unread,
Mar 25, 2017, 1:12:33 PM3/25/17
to
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.

Alvin

unread,
Mar 25, 2017, 1:39:08 PM3/25/17
to
On 2017-03-25 18:12, bitrex wrote:
> As Alf P. says for truly gigantic constant lookup tables it probably
> doesn't make sense to use C++ itself to generate them.

The thing is, it works very well when using my own array class with the
proper constexpr members. A 1'000'000 array size still has very decent
compile time performance.

Not being able to use std::array (the custom array class is still usable
as plain C-style array) is not nearly as bad as having to resort to some
external code generation tool.
0 new messages