function object with lambda
Not sure how to force a for_each with a lambda to invoke the random
function object successively. In the for_each version below get_long is
called just once. Not sure how to use var, std_function, bind or
something else to overcome this.
I'm sure the answer is straight forward... anyone?
Regards,
Matt.
#include <iostream>
#include
In our last exciting episode "Matt Hurd" wrote:
Not sure how to force a for_each with a lambda to invoke the random function object successively. In the for_each version below get_long is called just once. Not sure how to use var, std_function, bind or something else to overcome this. I'm sure the answer is straight forward... anyone?
for_each(v.begin(),v.end(), _1 = gen_long() );
Here, gen_long() is invoked and the resulting value is stored in the lambda functor, which is passed to for_each. You need to delay the call of gen_long with bind: for_each(v.begin(), v.end(), _1 = bind(gen_long)); Jaakko
G'day Jaakko, That was quick. Thanks.
Here, gen_long() is invoked and the resulting value is stored in the lambda functor, which is passed to for_each. You need to delay the call of gen_long with bind:
for_each(v.begin(), v.end(), _1 = bind(gen_long));
Jaakko
Yep, that's how I originally had it. Generates lots of compile errors with GCC 3.2 (mingw). Tried the gen_long() version with VC7 and Intel 7.1 and that will not work. Nor will the bind version. Any clues? Regards, Matt.
In our last exciting episode "Matt Hurd" wrote:
That was quick. Thanks.
Don't mention.
for_each(v.begin(), v.end(), _1 = bind(gen_long));
Yep, that's how I originally had it. Generates lots of compile errors with GCC 3.2 (mingw).
Any clues?
There seems to be two things. First, LL can't deduce the return type of gen_long() call. That is a bit odd, as the class seems to define result_type, which should be enough. Anyway, that can be fixed with bind<long>(gen_long) Then your gen_long is a stateful object, and the operator() is not defined as const. Lambda functors store all arguments by default as const copies. You must thus tell the library to store a reference to your original function object: bind<long>(ref(gen_long)) var does just as well: bind<long>(var(gen_long)) Cheers, Jaakko
-----Original Message----- From: Jaakko Jarvi [mailto:jajarvi@cs.indiana.edu] <snip>
Any clues?
There seems to be two things. First, LL can't deduce the return type of gen_long() call. That is a bit odd, as the class seems to define result_type, which should be enough. Anyway, that can be fixed with
bind<long>(gen_long)
Then your gen_long is a stateful object, and the operator() is not defined as const. Lambda functors store all arguments by default as const copies. You must thus tell the library to store a reference to your original function object:
bind<long>(ref(gen_long))
var does just as well:
bind<long>(var(gen_long))
Cheers, Jaakko
Beautiful. Thanks mate. Matt.
participants (2)
-
Jaakko Jarvi
-
Matt Hurd