Search code examples
c++mapreduce

Why do we have std::transform_reduce in C++ standard library?


We have std::transform and std::reduce functions in C++ standard library . What advantages do we have (except shortening the code) if we use std::transform_reduce instead of a combination of std::transform and std::reduce?


Solution

  • Iterator-based <algorithm> cannot be combined without a temporary copy.

    They could, if the functions returned lazy iterators to the transformed ranges, but for better or worse that is not how those functions were designed.

    So instead you get the XXX_YYY that do both things without extra copies, but not all combinations are available and they still do not scale to multiple operations.

    Allowing to combine operations into a computational pipeline of sorts is one of the main advantages of <ranges>.