本文介绍了如何将std :: function对象传递给带有函数指针的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与使用 c 编写的库交互,该库使用以下熟悉的模式:

  void some_c_handler(void(* func)(void *),void *数据); 

现在,我想写一个 C ++ 该函数的包装如下所示:

  void my_new_cpp_handler(std :: function< void()&& func)
{
void(* p)()= foo(func);
void * data = bar(func);
some_c_handler(p,data);
}

两者 some_c_handler my_new_cpp_handler 正在解决相同的问题;他们承担着某种功能以及某种状态。但是最好使用后者,因为它可以从用户那里提取很多实现细节,并允许简单地传入lambda对象。



所以 my_new_cpp_handler 应该使用给出的 func 参数,将其转换为函数指针,并将其状态传递给 data



我对标准或 std :: function 知道这是否是合理的请求。 foo bar 是否可以存在?



换句话说,我想要的是能够将有状态函数传递给 c 回调处理程序,而无需手动实例化我自己的 struct 类型与之一起传递。显然 std :: function 已经为我们完成了此操作,因此我很希望能够以某种方式将函数指针与状态分离,并将其传递给 c 样式的处理程序。

解决方案

否。



您可以将C样式的回调包装到 std :: function< ;> ... ,编译器将发出代码以提取 data handler 并调用它。但是,执行此操作的确切代码取决于编译器, ABI 和所使用的标准C ++库。仅使用 std :: function 包装器,您无法神奇地重构该代码。



此外,任意 std :: function ... (或lambda)可以包装代码 other ,而不是调用C样式处理程序。很明显,使用魔术重构代码从这样的 std :: function ... 实例中提取C样式处理程序不可能成功。 / p>

PS

那为什么不要,您只需使用 std :: function 已经为您完成的工作即可:

  void my_new_cpp_handler(std :: function< void()&& func)
{
func(); //调用 func编码的some_c_handler(p,data)IFF。
}


I am trying to interface with a library written in c, that uses this familiar pattern:

void some_c_handler(void(*func)(void*), void* data);

Now, I want to write a C++ wrapper for this function that looks like this:

void my_new_cpp_handler(std::function<void()>&& func)
{
  void (*p)() = foo(func);
  void* data = bar(func);
  some_c_handler(p, data);
}

Both some_c_handler and my_new_cpp_handler are solving the same problem; they're taking in some kind of function along with some state. But the latter is preferred in that it abstracts much of the implementation details from the user, and allows for simply passing in a lambda object.

So my_new_cpp_handler should take the func parameter it is given, convert it to a function pointer and pass its state on to data.

I don't know enough about the standard, or the implementation of std::function to know if this is even a reasonable request. Can foo and bar exist?

To put it differently, what I want is to be able to pass a stateful function to a c callback handler without having to manually instantiate my own struct type to pass along with it. Obviously std::function has already done this for us, so I'd love to be able to separate the function pointer from the state somehow and pass it onto the c-style handler. Is this possible?

解决方案

No.

You can wrap a C-style callback into an std::function<>..., and the compiler will emit code to extract the data and the handler and call it. However, the exact code to do so will depend on the compiler, the ABI and the standard C++ library being used. You can't magically reconstruct that code given only the std::function wrapper.

Further, an arbitrary std::function... (or a lambda) may wrap code other than a call to C-style handler. It should be obvious that applying the "magically reconstructed" code to extract C-style handler from such an instance of std::function... can't possibly succeed.

P.S.

So why don't you just use what std::function has already done for you:

void my_new_cpp_handler(std::function<void()>&& func)
{
  func();  // calls some_c_handler(p, data) IFF that is what "func" encodes.
}

这篇关于如何将std :: function对象传递给带有函数指针的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:03