Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do C++ functor constructors get called when used with for_each or std::transform

Tags:

c++

functor

I've never used c++ functors before and so I'm just trying to understand how they work.

e.g. suppose we have this functor class

class MultiplyBy {
private:
    int factor;

public:
    MultiplyBy(int x) : factor(x) { }

    int operator () (int other) const {
        return factor * other;
    }
};

Using it like this is clear to me:

MultiplyBy mult_3(3);

int x = mult_3(100);

Obviosuly the constructor of MultiplyBy is being called with the argument 3.

But in the following case, how is the constructor being called with the value in the array?

int array[5] = {1, 2, 3, 4, 5};
std::transform(array, array + 5, array, MultiplyBy(3));
like image 415
hookenz Avatar asked Jan 20 '23 04:01

hookenz


2 Answers

Well, in the final case, you are creating a new MultiplyBy object with 3 as the constructor argument. This object is then passed into std::transform, that it then calls operator() on.

If it helps you understand better, this is identical:

int array[] = {1, 2, 3, 4, 5};
MultiplyBy times3(3);
std::transform(array, array + 5, array, times3);
like image 128
Chris Jester-Young Avatar answered Mar 02 '23 02:03

Chris Jester-Young


You can think of transform being structured like this:

void transform(Iterator b, Iterator e, Functor f)
{
    for(;b != e; ++b)
    {
        *b = f(*b);
    }
}

The functor is passed by value into the function.
So when you call like this:

std::transform(array, array + 5, array, MultiplyBy(3));

Here you have created a temporary object. This is passed as a parameter value into transfer(). This results in the functor being copied into the function (not a problem since it has only a POD member and the compiler generated copy constructor works just fine). The parameter can then be used normally.

Note: The temporary object is destroyed at the end of the expression that it was created in (which will be after transform() returns).

like image 29
Martin York Avatar answered Mar 02 '23 03:03

Martin York