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

懵懂的女人 提交于 2021-02-08 12:16:59

问题


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.


回答1:


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



回答2:


TL;DR;

There is a proposal in process. We won't know the result until the process is done but it won't make C++20.

Longer answer

There was indeed a proposal: constexpr for algorithms, quoting the relevant part:

This proposal is to add constexpr to the following function templates in , excepting the function templates that accept an ExecutionPolicy.

  • accumulate

...

We can see from the cplusplus/papers issue 432 that the paper was moved to Language Evolution Working Group:

R0, needs to be looked at / forwarded by LEWG. Removing the LWG tag.

and the milestone was moved to 2019-11:

modified the milestones: 2019-07, 2019-11

which would be the upcomoing Belfast meeting, so it will not make C++20.



来源:https://stackoverflow.com/questions/58005977/why-has-stdaccumulate-not-been-made-constexpr-in-c20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!