如何解决如何参数化标准库算法的执行策略?
我调用了 sort
C++ 算法:
std::sort(std::execution::par_unseq,vec.begin(),vec.end());
现在我想参数化执行策略:
std::sort(executionPolicy,vec.end());
然而,std::execution::seq,std::execution::par,std::execution::par_unseq,std::execution::unseq
是不同类型的对象,分别为:std::execution::sequenced_policy,std::execution::parallel_policy,std::execution::parallel_unsequenced_policy,std::execution::unsequenced_policy
。
如何使用变量来存储属于不共享基类的不同类的对象?
解决方法
为此目的,我使用的方法是使用 std::variant
和 std::visit
。
using parallel_policy_holder = std::variant
<
std::execution::sequenced_policy,std::execution::parallel_policy,std::execution::parallel_unsequenced_policy,std::execution::unsequenced_policy
>;
void foo(parallel_policy_holder policy,std::vector<int> const& input)
{
std::visit
(
[&](auto policy_real) { std::sort(policy_real,input.begin(),input.end()); },policy
);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。