Uniquely identify a boost::function pointer

Started by
8 comments, last by Kylotan 6 years ago

Hello,

I have a function that looks like following:


void printCallbackAddress(boost::function<void()> myCallback)
{
    printf("%x",myCallback);	
}

The funny thing is that it will print a different address depending where I call above function. However I always call above function with the same callback argument, i.e. the same callback function. I guess this is linked to how the boost::function is working. But how can I uniquely identify a callback passed as argument? Because I need this so that a server can "remember" which callback was registered on a client.

Thanks for any input

Advertisement

Why do you think that you can extract an address by casting an object to a hexadecimal value? I don't think there's any guarantee that boost::function is a small wrapper around a pointer, and even if it were, I don't think it would guarantee that the first 4/8 bytes are necessarily that pointer.

If you want to know if 2 boost::function objects are the same, then that's what the equality operator is for. https://www.boost.org/doc/libs/1_66_0/doc/html/function/tutorial.html#id-1.3.15.5.8

 

25 minutes ago, codingJoe said:

I have a function that looks like following:



void printCallbackAddress(boost::function<void()> myCallback)
{
    printf("%x",myCallback);	
}

 

Passing a C++ object through C '...' (varargs) is completely undefined behaviour.  Avoid undefined behaviour, you never know what you're going to get. Follow Kylotan's advice.

Stephen M. Webb
Professional Free Software Developer

Thanks to both of you for your replies.

I am aware that you cannot directly print myCallback. But I need a function that allows to extract a unique string from myCallback.

The only solution I can think of right now is to keep a list of registered functions, and associate each one with a counter that gets incremented each time a new callback is registered.

Thanks

I still can't get it to work, maybe I misunderstood something.

Why does following not compile?


int myCallback(float value)
{
    return(0);
}

int main(int argc,char* argv[])
{
    boost::function<int(float)> a=myCallback;
    boost::function<int(float)> b=myCallback;
    if (a==b) // causes a compilation error
    {
    }
    return(0);
}

Or in other words: how would I have to write my code to check if a and b reference the same function?

This cannot be done in the general case, because boost::function is a generic wrapper around a callable object that may or may not be equality comparable.  Consider:


#include <boost/function.hpp>
#include <iostream>

  
struct uncomparable_fn() {
  void operator()() const { std::cout << "Hello world.\n" << std::flush; }
};
  
int main() {
  uncomparable_fn f1, f2;
  boost::function<void()> fn1(f1), fn2(f2);
  
  // The following line cannot compile, because uncomparable_fn has no operator==.
  if (f1 == f2) {
  }
  
  // Therefore the following line also cannot compile.
  if (fn1 == fn2) {
  }
}

 

"I need a function that allows to extract a unique string from myCallback."

Hang on -- let's explore that requirement a bit -- why do you think you need that?

 

2 hours ago, codingJoe said:


//[...]
    if (a==b) // causes a compilation error
//[...]

 

 

Why would you ever report that your code causes a compilation error without giving us the error message? It might not mean anything to you, but there's a decent chance someone here will understand it.

 

In addition, the docs imply that equality comparison should work. So the error is likely to be something else.

https://www.boost.org/doc/libs/1_66_0/doc/html/function/reference.html#header.boost.function_hpp

 

This topic is closed to new replies.

Advertisement