以下片段无法使用 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 whereis_trivially_copy_constructible_v<T>
andis_trivially_destructible_v<T>
are true.
但仅针对那些可以简单复制构造的元素,然后通过复制构造函数,而不是通过赋值。
关于c++ - unseq 执行策略是否要求迭代器的 value_type 为 Cpp17CopyAssignable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66562992/