c++ - 我可以在std::find_if的并行版本中使用非平凡的可迭代迭代器吗?

标签 c++ c++17

我有一个简单的代码,可以找到我自己的BigInt类型的任何分隔符,这些分隔符具有自己的随机访问迭代器:

  BigIntRangeIterator range(2, x);
  auto i = std::find_if(std::execution::par, range.begin(), range.end()
              , [&](auto y) {return x % y == 0;}
  );

我的迭代器包含std::vector,因此它不是普通可复制的。当我尝试编译此代码时,我从std::atomic实现中得到错误:
/usr/include/c++/9/atomic:191: error: static assertion failed: std::atomic requires a trivially copyable type
  191 |       static_assert(__is_trivially_copyable(_Tp),
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

这是否意味着在std::find_if函数的并行版本中只能使用平凡可复制的迭代器?

最佳答案

是的,它需要使用std::atomic来避免数据争用,而std::atomic的主要模板要求该类可以被普通复制构造。

https://en.cppreference.com/w/cpp/atomic/atomic#Primary_template

关于c++ - 我可以在std::find_if的并行版本中使用非平凡的可迭代迭代器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61303116/

相关文章:

c++ - 从异构列表中提取数据

c++ - 使用 constexpr 初始化非常量静态字符串

c++ - 在 MathGL 中使用 mglData 导致段错误

c++ - 错误 C2228、错误 C2275

c++ - C++ 中哪些类型被认为是可调用的?

c++ - 将参数包解压到字符串 View 中

C++ Mingw32 CreateProcess() 失败,错误代码 2 : The system cannot find the file specified

c++ - Windows运行时库依赖和纯C代码

c++ - libstdc++ 没有实现 std::stoi 吗?

c++ - 识别模板中的值是 bool