I recently got a problem need to integrate C++ 11 code written with lambda expression to old code base which only support C++ 98 compiler. I figured out couple of possible equiv
With Functor:
struct Functor
{
explicit Functor(uint attributes) : attributes(attributes) {}
void operator () (uint index, void * objectAddress) const
{
if ((ObjectInfo(index) & attributes) != 0)
{
fn(index, objectAddress);
}
}
uint attributes;
};
And then call
uint attributes = 0x0030;
// ....
ForEachObject(Functor(attributes));
For each different lambda, you have to write a functor.
You don't have to modify ForEachObject
Can Lamda expression be downgrade to C++ 98
No they cannot. Prior C++11 standards have no notion of lambda syntax.
Though there are surrogates available like boost::lambda
You can provide functor style classes, overriding the call operator (<return_type> operator()(<args>);
), to provide the same effect, as mentioned in the other answer.