ryanhaining / cppitertools

Implementation of python itertools and builtin iteration functions for C++17
https://twitter.com/cppitertools
BSD 2-Clause "Simplified" License
1.37k stars 115 forks source link

Unable to use iterator from combination with C++17 STL transform_reduce #80

Closed remz1337 closed 3 years ago

remz1337 commented 3 years ago

Hello, I'm trying to use the transform_reduce function from the standard library with the iterator given by the combination function of this library but I'm getting the following error:

Error C2338 Parallel algorithms require forward iterators or stronger.

You can reproduce with this code:


#include <cppitertools/combinations.hpp>
#include <numeric>//for reduce
#include <execution>//for parallel execution of reduce

int main(){
  std::vector<std::string> v{ "Mickey","Minnie","Jerry" };
  auto it2 = iter::combinations(v, 2);

  auto mapreduce = std::transform_reduce(
      std::execution::seq,
      it2.begin(),
      it2.end(),
      size_t{ 0 },
      [](const std::string& m) {// Transform 
          return m.size(); },
      [](size_t a, size_t b) {// Reduce 
              return a + b; }
      );
}
ryanhaining commented 3 years ago

Switch to ForwardIterator, (or having it conditionally) comes up occasionally. It especially makes sense for the combinatorics which require underlying ForwardIterators, but unfortunately (and surprisingly) the switch would break some existing uses.