c++ - unseq 执行策略是否要求迭代器的 value_type 为 Cpp17CopyAssignable?

标签 c++ c++17 stl-algorithm

以下片段无法使用 GCC 10 ( Compiler Explorer link ) 编译:

#include <vector>
#include <algorithm>
#include <execution>

struct T
{
  int const ID; // Not Cpp17CopyAssignable
};

int f(std::vector<T> const &v)
{
  if (v.empty()) return -1;
  
  return std::min_element(std::execution::par_unseq, v.begin(), v.end(),
                          [](T const &lhs, T const &rhs) { return lhs.ID < rhs.ID; })->ID;
}

因为 T 不是 Cpp17CopyAssignable:

 error: use of deleted function 'T& T::operator=(const T&)'
  643 |                 __min_val = __obj.__min_val;
      |                 ~~~~~~~~~~^~~~~~~~~~~~~~~~~

我在 cppreference 或 [algorithms] 中找不到这样的要求.我错过了吗?

最佳答案

C++ 标准不要求传递给并行算法的序列值是可赋值的(也不是可复制构造的,也不是默认构造的),除非非并行算法要求。不接受此类值的实现是不合格的。

[algorithms.parallel.defns]/2 Parallel algorithms access objects indirectly accessible via their arguments by invoking the following functions:
...
(2.2) — Operations on those sequence elements that are required by its specification.
...

这表示算法对值类型的要求不应超出必要范围。

有时允许并行算法复制元素:

[algorithms.parallel.exec]/2 Unless otherwise stated, implementations may make arbitrary copies of elements (with type T) from sequences where is_trivially_copy_constructible_v<T> and is_trivially_destructible_v<T> are true.

但仅针对那些可以简单复制构造的元素,然后通过复制构造函数,而不是通过赋值。

关于c++ - unseq 执行策略是否要求迭代器的 value_type 为 Cpp17CopyAssignable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66562992/

相关文章:

c++ - 具有空洞初始化的对象的生命周期

c++ - Clang 说 is_same_v<int, double> 的用法不是 constexpr,不知道为什么不是

c++ - 使用标准算法从容器中移除对象

c++ - 为什么 std::find_if(first, last, p) 不通过引用获取谓词?

c++ - 如何将 CComVariant bstr 转换为 CString

c++ - 在#define 中使用移位运算符的优点

c++ - 指向对象生命周期之外分配的内存的指针是 "invalid pointer[s]"还是 "pointer[s] to an object"?

c++ - 逆序获取 `std::priority_queue`个元素?

c++ - 弗莱施程序 C++

c++ - 将任何数据类型打包到 vector <uint8_t>