gpt4 book ai didi

c++ - 为什么 std::accumulate 在 C++20 中没有被设为 constexpr?

转载 作者:行者123 更新时间:2023-12-02 23:40:40 24 4
gpt4 key购买 nike

在 C++20 中,许多(大多数?)C++ 标准库算法已被制定为 constexpr。然而-std::accumulate has not .

seems like它可能是:

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

那么,它没有被 constexpr 化也是有原因的吗?

注意:这个问题的动机是 my answerthis question关于编译时累积。

最佳答案

P1645R1实际上是在贝尔法斯特 session 上通过的,以纳入 C++20,以响应 NB 评论 US 320 .

因此,以下所有算法都将是 C++20 中的 constexpr(除了采用 ExecutionPolicy 的重载):

  • 积累
  • 减少
  • 内部产品
  • transform_reduce
  • partial_sum
  • exclusive_scan
  • inclusive_scan
  • transform_exclusive_scan
  • transform_inclusive_scan
  • adjacent_difference
  • 伊奥塔

关于c++ - 为什么 std::accumulate 在 C++20 中没有被设为 constexpr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58005977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com