Search code examples
c++stdc++20accumulate

Why has std::accumulate not been made constexpr in C++20?


In C++20, many (most?) C++-standard-library algorithms have been made constexpr. Yet - std::accumulate has not.

It seems like it could have been:

template<class InputIt, class T>
constexpr T accumulate(InputIt first, InputIt last, T init)
{
    for (; first != last; ++first) {
        init = std::move(init) + *first;
    }
    return init;
}

So - is there a reason it wasn't constexpr'ed as well?

Note: This question was motivated by my answer to this question on compile-time accumulation.


Solution

  • P1645R1 was actually adopted in the Belfast meeting for inclusion in C++20 in response to NB comment US 320.

    As a result, all of the following algorithms will be constexpr in C++20 (except for the overloads of them that take an ExecutionPolicy):

    • accumulate
    • reduce
    • inner_product
    • transform_reduce
    • partial_sum
    • exclusive_scan
    • inclusive_scan
    • transform_exclusive_scan
    • transform_inclusive_scan
    • adjacent_difference
    • iota