I recently got a problem need to integrate C++ 11 code written with Lamda expression to old code base which only support C++ 98 compiler. I figured out couple of possible equivalences of Lamda like Macro, functor or function pointer. But seems they are all limited when translating Lamda with capture. For example a simple generic function with call back:
template <class Fn>
void ForEachObject(Fn fn)
{
for (uint i = 0; i < objectCount; i++)
{
fn(i, address + i * objectSize);
}
}
and the typical caller will do something like:
uint attributes = 0x0030;
....
ForEachObject([=](uint index, void * objectAddress)
{
if ((ObjectInfo(index) & attributes) != 0)
{
fn(index, objectAddress);
}
});
Note attributes here is come from out of the scope of Lamda. Is there anyway to still reuse the for each logic without Lamda? Or I must re-write the logic on every such caller?
Aucun commentaire:
Enregistrer un commentaire