unseq 执行策略是否要求迭代器的 value_type 是 Cpp17CopyAssignable?
Do the unseq execution policy require the iterators' value_type to be Cpp17CopyAssignable?
以下片段无法使用 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.
但仅针对那些可简单复制构造的元素,然后通过复制构造函数,而不是通过赋值。
以下片段无法使用 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.
但仅针对那些可简单复制构造的元素,然后通过复制构造函数,而不是通过赋值。