copy(...) 和 copy(seq, ...) 的关系
Relationship between copy(...) and copy(seq, ...)
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)
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)