gpt4 book ai didi

c++ - copy(...) 和 copy(seq, ...) 的关系

转载 作者:行者123 更新时间:2023-11-30 03:14:47 32 4
gpt4 key购买 nike

std::copy 与执行策略参数之间是否存在正式关系?无论是在实践中还是在标准中。

例如,会不会只是这样,

namespace std{
template<class It>
It copy(std::execution::sequenced_policy, It first, It last, It d_first){
return std::copy(first, last, d_first);
}
}

namespace std{
template<class It>
It copy(std::execution::sequenced_policy, It first, It last, It d_first){
// using std::copy; // may not be needed
return copy(first, last, d_first);
}
}

请注意,在第一个版本中,意味着我还需要重载 copy(par::seq, ...)

或者是这样

namespace std{
template<class It>
It copy(std::execution::sequenced_policy, It first, It last, It d_first){
... not defined at all in terms of other `copy(It, ...)` or `std::copy(It, ...)`
}
}

原因是我想为一种特殊的迭代器重载复制算法(在自定义命名空间中)。

最佳答案

一个区别在[execpol.seq]中提到是

During the execution of a parallel algorithm with the execution​::​sequenced_­policy policy, if the invocation of an element access function exits via an uncaught exception, terminate() shall be called.

演示:

#include <execution>
#include <iostream>
#include <stdexcept>

struct C {
C() {}
C& operator=(const C&) {
throw std::runtime_error("copy failed");
}
};

int main() {
C a[1];
C b[1];

try {
std::copy(std::begin(a), std::end(a), std::begin(b));
} catch(const std::runtime_error& ex) {
std::cout << "Exception: " << ex.what() << "\n";
}

try {
std::copy(std::execution::seq, std::begin(a), std::end(a), std::begin(b));
} catch(const std::runtime_error& ex) {
std::cout << "Exception: " << ex.what() << "\n";
}
}

可能的输出:

Exception: copy failed
terminate called after throwing an instance of 'std::runtime_error'
what(): copy failed
Aborted (core dumped)

关于c++ - copy(...) 和 copy(seq, ...) 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57514182/

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