Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1266

Answer by HolyBlackCat for How can I initialise a constexpr array with values using std::generate

$
0
0

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;}();

Viewing all articles
Browse latest Browse all 1266

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>