mardi 24 août 2021

How can I define a function that takes a lambda with capture as parameter?

I have this function that returns true if any of the elements in the list match the given predicate

bool _any(bool (*predicate)(MyStructure), list<MyStructure> arr)
{
    for (int i = 0; i < arr.size(); i++)
    {
        if (predicate(arr.front()))
            return true;
        arr.pop_front();
    }
    return false;
}

This works for very simple lambdas, but if the given lambda needs to capture this then I have an error which I can't figure out how to fix.

Assert::IsTrue(_any(
    [this](MyStructure t)
    {
        return t._name == "NAME_SEARCHED" &&
            t._type == "TYPE_SEARCHED" &&
            _any([](OtherStruct o) { return o._name == "SEARCHED_2"; }, t._children);
    },
    myList));

Error:

cannot convert argument 1 from 'UTests::<lambda_e8bda0383e9f0c2ae44be631a7424852>' 
to 'bool (__cdecl *)(MyNameSpace::MyStructure)'

(N.B. : _any that takes an OtherStruct is defined as well).

Aucun commentaire:

Enregistrer un commentaire