简体   繁体   中英

Why am I getting an error with MSVC when using a C++17 parallel execution algorithm on Boost zip iterators?

I have some trouble using C++17 parallel execution algorithm with Boost iterators on MSVC. Here is my code:

#include <vector>
#include <execution>
#include <boost/range/combine.hpp>

int main(void)
{
  std::vector<double> const v1(20, 1);
  std::vector<double> const v2(20, 2);
  std::vector<double> v_out;
  v_out.resize(20);

  auto const & combi = boost::combine(v1, v2);

  auto const run = [](auto const & v)
  {
    return boost::get<0>(v) + boost::get<1>(v);
  };

  std::transform(std::execution::par, combi.begin(), combi.end(), v_out.begin(), run);

  return 0;
}

I get the following error: error C2338: Parallel algorithms require forward iterators or stronger.

This seems due to the Boost zip iterator but I don't understand why since it compiles without error on GCC. What am I missing here? Is this a bug implementation in Visual C++?

zip iterators can only be input in the C++17 iterator hierarchy because their reference types are not real references.

It is undefined behavior to passing an input iterator to a parallel algorithm. MSVC's implementation happens to check the precondition more aggressively than GCC's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM