How can I initialise a constexpr array with values using std::generate

1.3k Views Asked by At

For example, if I wanted a constexpr std::array<int,100> initialised with all the multiples of 3 from 1-300 at compile time how can I do this?

My first thought was to use std::generate, something like:

constexpr std::array<int,100> a { std::generate(a.begin(), a.end(), [n=0]()mutable{ return n+=3; });

I get an error such as <source>:9:52: error: void value not ignored as it ought to be

and I can't use std::generate after this because of course, it's read only at that point

Thanks for any help

2

There are 2 best solutions below

1
On

The trick is to put the code into an immediately-invoked lambda. Then it doesn't matter if you use std::generate or a plain loop:

constexpr std::array<int,100> a = []{
    std::array<int,100> ret{};
    std::generate(ret.begin(), ret.end(),  [n=0]() mutable {return n += 3;});
    return ret;
}();
constexpr std::array<int,100> a = []{
    td::array<int,100> ret{};
    for (std::size_t i = 0; i < ret.size(); i++)
        ret[i] = 3 * (1+i);
    return ret;
}();
3
On

You can use index_sequence:

template <size_t ... Indices>
constexpr auto gen(std::index_sequence<Indices...>) {
    return std::array<int,100>{ (Indices * 3)... };
}

int main() {
    constexpr std::array<int,100> a = gen(std::make_index_sequence<100>());
}