[lambda] Using lambda expressions with STL function adaptors
This little piece of code,
#include <algorithm>
#include <functional>
template
AMDG Robert Jones wrote:
Gives a compiler error, which I think tells me that my lambda expression, _1 == 3 does not provide the argument_type typedef. I thought lambda expressions were careful to to do this. What am I doing wrong?
Lambda can't provide argument_type typedefs in general. Consider the following: (_1 == 3)(1.0) (_1 == 3)(10) Both of these are legal. The same function object can be called with either a double or an int. There is no unique argument type. In Christ, Steven Watanabe
On Sat, Jul 19, 2008 at 5:31 AM, Steven Watanabe
Lambda can't provide argument_type typedefs in general.
Consider the following:
(_1 == 3)(1.0) (_1 == 3)(10)
Both of these are legal. The same function object can be called with either a double or an int. There is no unique argument type.
Yes, the logic of that is undeniable, so I'm going to backtrack a bit as somewhere my understanding has gone astray. What I think I know: * That boost::lambda::bind( ) creates 'conformant' functors. What I've assumed from what I know: * That 'conformant' includes defining the argument_type typedef * That what is true for bind is also true lambda expressions. Which of these thing is true? Thanks, Rob.
AMDG Robert Jones wrote:
Yes, the logic of that is undeniable, so I'm going to backtrack a bit as somewhere my understanding has gone astray.
What I think I know:
* That boost::lambda::bind( ) creates 'conformant' functors.
It depends on what you mean by conformant. Algorithms like for_each do not require these typedefs. Adapters like not1 do. If you need to adapt an arbitrary function object, you're safer using !boost::bind<bool>(pred) instead of std::not1. This will also work with function pointers. (Which std::not1 doesn't handle)
What I've assumed from what I know:
* That 'conformant' includes defining the argument_type typedef
Again, it depends on what you mean by conformant.
* That what is true for bind is also true lambda expressions.
Meaning lambda::bind? The result of lambda::bind /is a/ lambda expression. In Christ, Steven Watanabe
On Sat, Jul 19, 2008 at 9:24 PM, Steven Watanabe
It depends on what you mean by conformant. Algorithms like for_each do not require these typedefs. Adapters like not1 do. If you need to adapt an arbitrary function object, you're safer using !boost::bind<bool>(pred) instead of std::not1. This will also work with function pointers. (Which std::not1 doesn't handle)
If only I could! Using Boost throughout would be far-and-away the best solution, trouble is at the point of definition of if_all( ) I don't have, and can't make, Boost available, whereas at the point of use I have. Thanks for all your help. Rob.
participants (2)
-
Robert Jones
-
Steven Watanabe