0

I'm trying to get a pointer to a function in this way:

 boost::function<void(float)> Function;
 Function = boost::bind(&myClassType::myMemberFunction, this, _1);
 void(*)(float) finalFunction = *Function.target<void(*)(float)>();
/* crash becouse Function.target<void(*)(bool)>() is null */

But I can not get the pointer. Where I am wrong? Did I do something that is not allowed?

(I have to pass finalFunction to lua_register.)

  • Thing about what you're trying to do. A function pointer can only point at an existing function somewhere in compiled code, yet you want one to point at a dynamically-binded function; not possible. You need to use upvalues for your Lua functions. – GManNickG Apr 28 '11 at 02:30

1 Answers1

1

function::target() is defined (I'm using the C++11 draft, which I think is a bit more clear than boost's reference) as follows:

per C++0x n3290 20.8.11.2.5[func.wrap.func.targ]/3

Returns: If target_type() == typeid(T) a pointer to the stored function target; otherwise a null pointer.

In your case, T is your type void(*)(float), but Function.target_type() is not that at all, it is the type of the boost expression used to initialize boost::function.

So in short, yes, this is not allowed. The workarounds are not obvious, but here's one: demote boost::function to a plain function pointer

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • I tried it, but with no success :( I got this error: "invalid static_cast from type ‘void*’ to type ‘int (*)(lua_State*)’" –  Apr 28 '11 at 12:15