<i id='H0UG4'><tr id='H0UG4'><dt id='H0UG4'><q id='H0UG4'><span id='H0UG4'><b id='H0UG4'><form id='H0UG4'><ins id='H0UG4'></ins><ul id='H0UG4'></ul><sub id='H0UG4'></sub></form><legend id='H0UG4'></legend><bdo id='H0UG4'><pre id='H0UG4'><center id='H0UG4'></center></pre></bdo></b><th id='H0UG4'></th></span></q></dt></tr></i><div id='H0UG4'><tfoot id='H0UG4'></tfoot><dl id='H0UG4'><fieldset id='H0UG4'></fieldset></dl></div>

    <small id='H0UG4'></small><noframes id='H0UG4'>

    • <bdo id='H0UG4'></bdo><ul id='H0UG4'></ul>
  1. <tfoot id='H0UG4'></tfoot>

  2. <legend id='H0UG4'><style id='H0UG4'><dir id='H0UG4'><q id='H0UG4'></q></dir></style></legend>
    1. 如何将 C++ lambda 传递给需要函数指针和上下文的 C 回调?

      时间:2023-06-30
        <legend id='WBYTh'><style id='WBYTh'><dir id='WBYTh'><q id='WBYTh'></q></dir></style></legend>

        <i id='WBYTh'><tr id='WBYTh'><dt id='WBYTh'><q id='WBYTh'><span id='WBYTh'><b id='WBYTh'><form id='WBYTh'><ins id='WBYTh'></ins><ul id='WBYTh'></ul><sub id='WBYTh'></sub></form><legend id='WBYTh'></legend><bdo id='WBYTh'><pre id='WBYTh'><center id='WBYTh'></center></pre></bdo></b><th id='WBYTh'></th></span></q></dt></tr></i><div id='WBYTh'><tfoot id='WBYTh'></tfoot><dl id='WBYTh'><fieldset id='WBYTh'></fieldset></dl></div>
      1. <tfoot id='WBYTh'></tfoot>
              <tbody id='WBYTh'></tbody>
                <bdo id='WBYTh'></bdo><ul id='WBYTh'></ul>

                <small id='WBYTh'></small><noframes id='WBYTh'>

                本文介绍了如何将 C++ lambda 传递给需要函数指针和上下文的 C 回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试在使用标准函数指针+上下文范例的 C-API 中注册回调.这是 api 的样子:

                I'm trying to register a callback in a C-API that uses the standard function-pointer+context paradigm. Here's what the api looks like:

                void register_callback(void(*callback)(void *), void * context);
                

                我真正想做的是能够将 C++ lambda 注册为回调.此外,我希望 lambda 是一个已捕获变量(即不能转换为直接无状态的 std::function)

                What I'd really like to do is be able to register a C++ lambda as the callback. Additionally, I want the lambda to be one that has captured variables (ie. can't be converted to a straight stateless std::function)

                我需要编写什么样的适配器代码才能将 lambda 注册为回调?

                What kind of adapter code would I need to write to be able to register a lambda as the callback?

                推荐答案

                简单的方法是将 lambda 粘贴到一个 std::function 中,并保存在某处.可能它是在堆上分配的,并且仅被注册到接受回调的实体的 void* 引用.回调将只是一个像这样的函数:

                The simple approach is to stick the lambda into a std::function<void()> which is kept somewhere. Potentially it is allocated on the heap and merely referenced by the void* registered with the entity taking the callback. The callback would then simply be a function like this:

                extern "C" void invoke_function(void* ptr) {
                    (*static_cast<std::function<void()>*>(ptr))();
                }
                

                请注意,std::function 可以保存具有状态的函数对象,例如,具有非空捕获的 lambda 函数.您可以像这样注册回调:

                Note that std::function<S> can hold function objects with state, e.g., lambda functions with a non-empty capture. You could register a callback like this:

                register_callback(&invoke_function,
                  new std::function<void()>([=](){ ... }));
                

                这篇关于如何将 C++ lambda 传递给需要函数指针和上下文的 C 回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tbody id='nzzEz'></tbody>
              • <tfoot id='nzzEz'></tfoot>

                  <small id='nzzEz'></small><noframes id='nzzEz'>

                  <i id='nzzEz'><tr id='nzzEz'><dt id='nzzEz'><q id='nzzEz'><span id='nzzEz'><b id='nzzEz'><form id='nzzEz'><ins id='nzzEz'></ins><ul id='nzzEz'></ul><sub id='nzzEz'></sub></form><legend id='nzzEz'></legend><bdo id='nzzEz'><pre id='nzzEz'><center id='nzzEz'></center></pre></bdo></b><th id='nzzEz'></th></span></q></dt></tr></i><div id='nzzEz'><tfoot id='nzzEz'></tfoot><dl id='nzzEz'><fieldset id='nzzEz'></fieldset></dl></div>
                    <bdo id='nzzEz'></bdo><ul id='nzzEz'></ul>

                      • <legend id='nzzEz'><style id='nzzEz'><dir id='nzzEz'><q id='nzzEz'></q></dir></style></legend>