Boost logo

Boost Users :

Subject: Re: [Boost-users] [Bind & Function] c function pointers
From: Lars Viklund (zao_at_[hidden])
Date: 2013-11-29 07:37:24


On Fri, Nov 29, 2013 at 11:23:35AM +0100, Matthijs Möhlmann wrote:
> Hello all,
> I'm not sure if this is possible at all, but let me explain:

It's not possible.

> I have the following function signature:
> typedef int (*sasl_callback_ft)(void);
>
> That function in question gets called with unknown amount of parameters
> (usually 3 or 4)

That doesn't quite make sense. That signature is for a function that
takes exactly zero arguments, both in C and C++.

> boost::function<int(void *, int, const char **, unsigned *)> callback =
> boost::bind(&admin::callback_fun, this, _1, _2, _3, _4);
>
> Obviously the compiler doesn't allow to assign callback to a
> sasl_callback_ft var
> sasl_callback_ft fun = callback;

Both boost::function and the return type of boost::bind are objects of a
Callable type. They bear no relation to traditional function pointers
except that you can call them with the same kind of syntax.

A function pointer can be assigned a free function, a static member
function or a stateless lambda.

You may want to look into libraries that via code generation or other
tricks produce a completely new function in memory, often called a
"thunk" or a "trampoline", which forwards to the actual more complex
callable.

Another approach is to store state in some external/global/thread-local
storage and assign a free function that forwards the call based on the
stored state and any other state you can gleam from your library.

Often, C-like APIs like the one you try to interact with tends to have a
void* in the interface which you can use to tunnel the information
needed to call the right thing. If this API doesn't have such a thing,
you're in for a fun time.

-- 
Lars Viklund | zao_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net