2

I have the following function

typedef std::function<void(const data<3> & data, void * userData )> callbackFnc;

and some structure

typedef struct
{
  callbackFnc callback;
  void * userData;
} callbackData;

I store all these inside of my class in vector

std::vector<std::shared_ptr<callbackData> > mCallbacks;

This is how I add it:

bool MyClass::addCallback(callbackFnc cbFunc, void * userData)
{
std::shared_ptr<callbackData> cb = std::make_shared<callbackData>();
    cb->callback = cbFunc;
    cb->userData = userData;

    mCallbacks.push_back(cb);
}

Now I want to check that the given function has been added and remove it. To do this I just compare function's address like this:

for (auto it = mCallbacks.begin(); it != mCallbacks.end(); it++)
{
    // Compare address of these two functions
    if ((&(*it)->callback) == &cbFunc)
    {
        mCallbacks.erase(it);

        result = true;
        break;
    }
}

Looks like this comparison is not correct, but I can't figure out why. Any advice will be useful.

Thanks!

Bill Lumbert
  • 4,633
  • 3
  • 20
  • 30
  • You compare the address of the `callback` member with the address of a function pointer. You should compare the content of `callback` with the (address of the) function. `(*it)->callback == cbFunc` – mch Jul 23 '18 at 11:13
  • You seem to be using `std::function` like it was a function pointer. It isn't, and it doesn't support comparisons with anything other than `std::nullptr_t` – Caleth Jul 23 '18 at 11:14
  • `cb->callback` is a copy of the original `std::function` you passed in, and `cbFunc` in the last snippet is probably yet another instance (but there's not enough context to say). These will never have the same address because they're different objects. Maybe [`std::function::target`](https://en.cppreference.com/w/cpp/utility/functional/function/target) could be useful, but then you're better off using plain function pointers. – Quentin Jul 23 '18 at 11:15

1 Answers1

9

A std::function is not a function pointer. You cannot compare std::functions like you compare function pointers. If you comparing two std::functions, and expecting them to compare equal to each if they've type-erased the same function, that's not going to work. A std::function can only be compared to a nullptr. This is the only defined equality operator for std::functions.

Your goal is to identify a std::function and remove it from a vector of installed callback functions. The usual way this kind of functionality is done is by assigning a separate label to a wrapped function, often a std::string that gives a unique name to each function, and then you find the function by name. It doesn't have to be a std::string, an enum will work just as well. The bottom line is that you will have to establish how your std::functions are identified "out of band".

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Could you give some examples on how to wrap a function with a string? Is it something like this https://stackoverflow.com/a/37545823/767523 ? – Dark Oct 05 '21 at 12:33
  • That's one approach, it's much simpler to use a `std::map` or `std::unordered_map` to map arbitrary labels to type-erased `std::function`s. – Sam Varshavchik Oct 05 '21 at 12:46