Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interesting little program

65 views
Skip to first unread message

Doug Mika

unread,
Jul 27, 2015, 1:13:25 PM7/27/15
to
I came upon this little program in a book. I like it, but I'm confused by one little thing: in the for_each(InputIterator first, InputIterator last, Function fn) construct, we basically call fn(*first). (where first is iterated)

However, in the program bellow I don't see where the parameter (*first) would be:

void do_work(unsigned id);
void f()
{
std::vector<std::thread> threads;
for(unsigned i=0;i<20;++i){
threads.push_back(std::thread(do_work,i));
}
std::for_each(threads.begin(),threads.end(), std::mem_fn(&std::thread::join));
}

that is:
1) std::mem_fn declares a functor(function object) where our function is &std::thread::join, but this function DOESN'T take a parameter, in particular, it doesn't take a parameter of type std::thread??

Victor Bazarov

unread,
Jul 27, 2015, 1:21:47 PM7/27/15
to
'mem_fn' is an adapter. It's a function (template) that returns an
object of a class template instantiated with arguments deduced from the
argument to the 'mem_fn' function. Take a look at how that class
template's 'operator()' is implemented. I could write it down for you,
but it's better if you find the actual implementation in your compiler
headers and study it.

V
--
I do not respond to top-posted replies, please don't ask

Doug Mika

unread,
Jul 27, 2015, 2:23:13 PM7/27/15
to
the definition of mem_fn goes as follows :

template <class Ret, class T>
/* unspecified */ mem_fn (Ret T::* pm);
Convert member function to function object
Returns a function object whose functional call invokes the member function pointed by pm.

The type of the returned object has the following properties:
Its functional call takes as first argument an object of type T (or a reference or a pointer to it) and, as additional arguments, the arguments taken by pm (if any). The effect of such a call with fn as first argument are the same as calling fn.*pm (or (*fn).*pm if fn is a pointer), forwarding any additional arguments.

If instead I call with fn2 as an argument instead of fn, must fn2 have a member function pm or simply satisfy that all conditions of *pm be met on the object fn2 (that all necessary member variables required by *pm exist in fn2)?

Victor Bazarov

unread,
Jul 27, 2015, 2:43:26 PM7/27/15
to
On 7/27/2015 2:22 PM, Doug Mika wrote:
> the definition of mem_fn goes as follows :
>
> template <class Ret, class T>
> /* unspecified */ mem_fn (Ret T::* pm);
> Convert member function to function object
> Returns a function object whose functional call invokes the member
function pointed by pm.

So, what object does it return? What does its definition look like?
How is *its* operator() implemented? Did you actually look, as I have
suggested, in the headers that came with your compiler?

> The type of the returned object has the following properties:
> Its functional call takes as first argument an object of type T (or
> a
reference or a pointer to it) and, as additional arguments, the
arguments taken by pm (if any). The effect of such a call with fn as
first argument are the same as calling fn.*pm (or (*fn).*pm if fn is a
pointer), forwarding any additional arguments.

?

> If instead I call with fn2 as an argument instead of fn, must fn2
> have a member function pm or simply satisfy that all conditions of
> *pm be met on the object fn2 (that all necessary member variables
> required by *pm exist in fn2)?

I have no idea what you're talking about.

Doug Mika

unread,
Jul 27, 2015, 2:47:35 PM7/27/15
to
Unfortunately, I currently don't have a working version of C++ installed on my machine, and I happen to use the cpp.sh/tutorialspoint compilers for my little academic challenges.

red floyd

unread,
Jul 27, 2015, 5:32:41 PM7/27/15
to
Victor is right that you should figure it out for yourself, but since
you don't seem to have headers..

Given:

class T {
public:
void f();
};

std::mem_fn(T::f) creates a functor object that has an operator() that
looks roughly like this (PSEUDOCODE):

std::mem_fn::operator()(T& t)
{
return t.f();
}

0 new messages