简体   繁体   English

转换 std::function<void ()> 作废 (*)()</void>

[英]Convert std::function<void ()> to void (*)()

So I have a member function in C++ that takes in an std::function<void> and I want to pass this into a wiringPi function:所以我在 C++ 中有一个成员 function 接受一个std::function<void>我想将它传递给一个wiringPi function:

void my_class::on_change(std::function<void()> func) const
{
    wiringPiISR(
        wiring_pi_pin_number_,
        INT_EDGE_BOTH,
        func);
}

I am getting the error cannot convert 'std::function<void()>' to 'void (*)()' in initialization .我收到错误cannot convert 'std::function<void()>' to 'void (*)()' in initialization I have looked online and I am only finding things that have a hundred lines of pointer and reference casting and do not compile on a pi.我在网上查看过,我只发现有一百行指针和引用转换并且不在 pi 上编译的东西。 Looking for any help here.在这里寻求任何帮助。 Should I be using void (*)() all the way up the stack?我应该一直使用void (*)()吗? This seems like the wrong approch.这似乎是错误的方法。 I can find plenty about converting a void (*)() to a std::function<void> but not the other way around..我可以找到很多关于将void (*)()转换为std::function<void>但不是相反的方法..

PS: I am a noob at c++. PS:我是 c++ 的菜鸟。 My main language is C# and some other higher languages so still learning this one and not sure what I am missing here..我的主要语言是 C# 和其他一些高级语言,所以仍在学习这门语言,不确定我在这里缺少什么..

Convert std::function<void ()> to void (*)()将 std::function<void ()> 转换为 void (*)()

You cannot.你不能。

Should I be using void (*)() all the way up the stack?我应该一直使用 void (*)() 吗?

Yes.是的。

Technically using std::function could work but you'd have to rely on global state and the benefits of using std::function may not be worth that cost.从技术上讲,使用 std::function 可以工作,但您必须依赖全局 state 以及使用 std::function 的好处可能不值得花这么多钱。

This seems like the wrong approch.这似乎是错误的方法。

It's not, given the limitations of the API that you use.鉴于您使用的 API 的限制,它不是。

Here is an example of how you can do it.这是一个如何做到这一点的例子。

template<class Sig> using pfunc_t=Sig*;
template<class Sig>
struct callback_entry{
  pfunc_t<Sig> pf = nullptr;
  std::function<Sig> f;
};
trmplate<class Sig, std::size_t N>
using callback_table = std::array< callback_entry<Sig>, N >;

template<class Sig, std::size_t N, callback_table<Sig,N>* ptable, class R, class...Args, std::size_t...Is>
callback_table<Sig,N> make_table( std::index_sequence<Is...> ){
  return {{
    {[](Args... args)->R{ return (*ptable)[Is].f(std::forward<Args>(args)); }}...
  }};
}

template<std::size_t N, class R, class...Args>
callback_table<R(Args...), N> callbacks = make_table<R(Args...), N, &callbacks<N,R,Args...>, R, Args...>(std::make_index_sequence<N>{});

template<std::size_t N, class R, class...Args>
pfunc_t<R(Args...)> register_callback( std::function<R(Args...)> f ){
  for(auto&& entry:callbacks<N,R,Args...>){
    if (entry.f) continue;
    entry.f=std::move(f);
    return entry.pf;
  }
}
template<std::size_t N, class R, class...Args>
void recycle_callback( pfunc_t<R(Args...)> pf ){
  for(auto&& entry:callbacks<N,R,Args...>){
    if (pf!=entry.pf) continue;
    entry.f=nullptr;
  }
}

lots of typos, and probably syntax errors, but I hope the idea is clear.很多错别字,可能还有语法错误,但我希望这个想法很清楚。

We make a global array of function pointers and std functions of fixed size.我们制作了一个由 function 指针和固定大小的 std 函数组成的全局数组。 The function pointers know their index, so they look up the std function and run it. function 指针知道它们的索引,因此它们查找标准 function 并运行它。 Registering looks for an unused std function, populates it, and returns the function pointer.注册查找未使用的 std function,填充它,并返回 function 指针。

Keep N low, as it eats up both executable code memory and runtime memory.保持 N 低,因为它会吃掉可执行代码 memory 和运行时 memory。 If it is too low you run out of callbacks however.但是,如果它太低,您将用完回调。

To convert std::function<void()> to void(*)() use the std::function::target<>() function.要将std::function<void()>转换为void(*)() ,请使用std::function::target<>() function。

The target function returns a pointer to the stored callable function target, as shown in code below.目标 function 返回指向存储的可调用 function 目标的指针,如下面的代码所示。

void f()
{
    cout << "f()" << endl;
}

void f1(void(*p)())
{
    p();
}

int main() {
    
    // Convert std::function<void()> to void(*)()
    
    std::function<void()> fp = &f;
    auto p1 = fp.target<void(*)()>();
    void (*p2)() = *p1;
    p2();
    
    f1(p2);
}

Output: Output:

f()

f()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将void *转换为std :: function <void()> - Convert void* to std::function<void()> 使用void函数作为回调,从“ std :: function转换 <void ()> ? - Using a void function as a callback, convert from "std::function<void ()>? 错误:无法将&#39;print&#39;从&#39;void(*)(int)&#39;转换为&#39;std :: function <void()> “ - error: could not convert ‘print’ from ‘void (*)(int)’ to ‘std::function<void()>’ std :: is_same用于void函数(...)和void类型? - std::is_same for void function(…) and void type? 有没有办法将 void (*)(uint32_t, void (*)(uint32_t)) 转换为 std::function <void(uint32_t, std::function<void(uint32_t)> )&gt;?</void(uint32_t,> - Is there a way to convert void (*)(uint32_t, void (*)(uint32_t)) to std::function<void(uint32_t, std::function<void(uint32_t)>)>? 在具有不同签名的std :: function之间转换(T * arg到void * arg) - Convert between std::function with different signatures (T* arg to void* arg) 无法将std :: bind的返回值转换为void函数指针 - unable to convert return value of std::bind to void function pointer 为什么我不能将[](auto &amp;&amp;…){}转换为std :: function <void()> ? - Why can't I convert [](auto&&…){} into a std::function<void()>? 转换std :: function <void(Derived*)> 到std :: function <void(Base*)> - Converting std::function<void(Derived*)> to std::function<void(Base*)> 转换std :: function <void(string, string)> 到一个通用的std :: function <void()> - Converting std::function<void(string, string)> to a generic std::function<void()>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM