I have a const std::vector<T> source
of a big size, and a std::vector<T> dest
.
I want to apply a transformation to each element in source
and store it in dest
; all of this in parallel, and knowing that T
is not default constructible.
What I initially attempted was using std::transform
with a parallel execution policy:
std::transform(std::execution::par_unseq,
source.begin(), source.end(),
dest.begin(),
[](const T& elem) { return op(elem); }
);
However, when I first compiled and run this, to my surprise, I discovered that, although the transform
"loops" for source.size()
times, dest
's content remains unchanged.
I discovered that this is because dest
must have the same size of source
prior to the transform
.
However, I cannot do resize dest
to the size of source
, because T
is not default constructible as it does not have a default constructor. I also would prefer not to provide a default constructor for it (first of all, it does not make sense in T
's logic, but you can think that it would be expensive to call).
Does C++ STL offer any other algorithm for achieving what I have in mind?
What would suffice is an algorithm where each thread computes its own part of the source
vector, and then the results are collected and joined into the same dest
vector.