How std::transform and std::plus work together?

std::plus<> is a functor, which is just fancy talk for a class that implements operator(). Here's an example:

struct plus {
    template <typename A, typename B>
    auto operator()(const A& a, const B& b) const { return a + b; }
};

The std::transform you have there is roughly equivalent to:

template<typename InputIt1, typename InputIt2, 
         typename OutputIt, typename BinaryOperation>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, 
                   OutputIt d_first, BinaryOperation binary_op)
{
    while (first1 != last1) {
        *d_first++ = binary_op(*first1++, *first2++);
    }
    return d_first;
}

Here, binary_op is the name given to std::plus<>. Since std::plus<> is a functor, C++ will interpret the "call" to it as a call to the operator() function, giving us our desired behavior.

Tags:

C++

C++11

C++14