back

by matt_d·9y ago·view on hn ↗
A `constexpr` function storing result in a `constexpr` variable (you need both for the compile-time evaluation guarantee): http://en.cppreference.com/w/cpp/language/constexpr

An extra complication in this example is that `std::accumulate` currently lacks `constexpr` support: http://stackoverflow.com/questions/32395408/why-arent-stdalg...

There's been a proposal to relax this limitation: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p020...

For now you could use a workaround in form of a range-based for loop: https://godbolt.org/g/aVEqlm or even https://godbolt.org/g/GDrFr5 (homogeneous types).

A fold expression is much cleaner, though (also supporting heterogeneous types): https://godbolt.org/g/Hrnkc1