Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector of std::functions find

Tags:

c++

c++11

I have a vector populated with callback functions and I would like to check whether callback to the function already exists prior to adding it. I don't know whether it will even work bu so far it doesn't even compile.

vector<std::function<void(void*)>> _callbacks;

void Event::RegisterCallback(std::function<void(void*)> callback)
{
    if (callback == NULL)
        return;

    vector<std::function<void(void*)>>::iterator it = std::find(_callbacks.begin(), _callbacks.end(), callback);

    if (it == _callbacks.end())
    {
        _callbacks.push_back(callback);
    }
    else
    {
        //print error
        throw;
    }
}

This gives a compile error: "Overload resolution selected deleted operator '=='" in alorithm(805). This is related to the find function call.

How do I get this to work and is it even going to compare function calls to the same method properly?

Thanks

like image 691
Chebz Avatar asked Sep 06 '13 20:09

Chebz


People also ask

How do you find something in a vector C++?

You can use the find function, found in the std namespace, ie std::find . You pass the std::find function the begin and end iterator from the vector you want to search, along with the element you're looking for and compare the resulting iterator to the end of the vector to see if they match or not.

How do you find an element in vector STL?

How do we find an element using STL? Approach 1: Return index of the element using std::find() Use std::find_if() with std::distance() Use std::count()

How do you return a vector from a function in C++?

Return by Value It is a preferred method to return vector from function in C++. It can reduce the execution time and space because in this method, an object is not copied for returning a vector. It only points the pointer to the vector which will be returned.

What will the find () function return if the element you're searching for isn't present in the vector VECT?

Return value The find() function returns an iterator that points to the val in the specified range. If the value is not found, then it returns an iterator to the last of the array or vector.


1 Answers

As noted in the comments the simplest solution is to use default C-style function pointers as they support == operator in opposite to C++11 function which does not.

using func_type = void(*)();
vector<func_type> _callbacks;

void Event::RegisterCallback(func_type callback)
{
    if (callback == nullptr)
       return;

    auto it = std::find(_callbacks.begin(), _callbacks.end(), callback);

    if (it == _callbacks.end()) {
        _callbacks.push_back(callback);
    }
    else {
        throw;
    }
}

void f() {};
void g() {};

/*
    evt.RegisterCallback(f); // works fine
    evt.RegisterCallback(g); // works fine
    evt.RegisterCallback(f); // throws exception
*/

If you don't like this approach you can write your own function-pointer class with support of equality operator.

like image 162
sasha.sochka Avatar answered Nov 15 '22 05:11

sasha.sochka