copy(...)和copy(seq,...)之间的关系

时间:2019-08-15 18:07:58

标签: c++ c++11 stl copy argument-dependent-lookup

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, ...)`
    }
}

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

1 个答案:

答案 0 :(得分:2)

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

  

在执行并行算法的过程中,   execution​::​sequenced_­policy策略(如果调用元素)   访问函数通过未捕获的异常退出,terminate()应该是   叫。

演示:

#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)