Question

I've written a function foreach that accepts a lambda function ala:

void foreach(void (*p)(pNode))
{ /* ... */ }

Which works as intended if I pass a lambda function from the main loop:

int a = 5;
env.N().foreach
(
    [&](pNode n)->void
    {
        n->tps(a); 
    }
);

However, if I try to call the same function from within a member method, the lambda function "inherits" the scope of the member function and generates a compiler error. For example, if I try to include it inside the member method of class Object named method(), I get the following error:

error: no matching function for call to ‘IDSet<Node>::foreach(Object::method()::<lambda(pNode)>)’
note: candidate is: void IDSet<T>::foreach(void (*)(IDSet<T>::pT)) [with T = Node, IDSet<T>::pT = pNode]

I realize this is the compiler being safe, since I could include instance-specific variables inside the lambda function, in which case the lambda would need to be scoped, however I'm wondering if it's possible to make this lambda "static".

I've tried a reinterpret_cast, however that gives me this error:

error: invalid cast from type ‘Object::method()::<lambda(pNode)>’ to type ‘void (*)(pNode)’

Specifying static before [&](pNode ... doesn't seem like valid syntax either.

Desperately, I also tried changing [&] to [=], [], [a], none of which worked.

Does anyone know if there is a way to do accomplish my goal of creating a "static" lambda function, or at any sort of lambda function that will be accepted for that matter?

Thanks!


Answer:

With help from Cat Plus Plus, I was able to turn my incorrect code:

void foreach(void (*p)(pT))
{
    for(pTiter i = _map.begin(); i != _map.end(); i++)
    {
        (*p)(i->second);
    }
}

into fully functional code:

void foreach(std::function<void(pT)>(p))
{
    for(pTiter i = _map.begin(); i != _map.end(); i++)
    {
        p(i->second);
    }
}

that does what I was looking for perfectly.

Was it helpful?

Solution

Well, you can not use pointers.

void foreach(std::function<void(pNode)>);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top