如何使用 C++ 静态类方法创建 C 风格的回调

整洁

大多数示例没有清楚地显示如何使用静态类方法创建 std::function,该方法将对象实例作为第一个参数。

我想采用一个静态类方法,该方法将对象实例作为第一个参数,并创建一个可以用作 C 样式回调函数并可以访问对象实例的新函数。

我似乎什么都试过了。我在这里拿了这个例子并试图通过在这里使用一个类似的例子将它重构到我的用例中,但没有运气

请参阅下面的更多最新示例

#include <functional>
#include <iostream>

struct Foo
{
    Foo(int me) : m_me(me) {}

    static int foo_static(Foo* f, int a, int b) { return f->m_me + a + b; }
    int m_me;
};

int main()
{
    Foo f(4);

    using std::placeholders::_1;
    std::function<int(int,int)> new_func = std::bind(&Foo::foo_static, &f, _1);

    std::cout << new_func(3, 4) << std::endl;
}

编辑

忘记编译器输出

$ c++ main.cpp -std=c++14
main.cpp:25:30: error: no viable conversion from '__bind<int (*)(Foo *, int, int), Foo *, const std::__1::placeholders::__ph<1> &>' to
      'std::function<int (int, int)>'
        std::function<int(int,int)> new_funct = std::bind(&Foo::foo_static, &f, _1);
                                    ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:1627:5: note: candidate constructor not
      viable: no known conversion from '__bind<int (*)(Foo *, int, int), Foo *, const std::__1::placeholders::__ph<1> &>' to 'std::nullptr_t'
      (aka 'nullptr_t') for 1st argument
    function(nullptr_t) _NOEXCEPT : __f_(0) {}
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:1628:5: note: candidate constructor not
      viable: no known conversion from '__bind<int (*)(Foo *, int, int), Foo *, const std::__1::placeholders::__ph<1> &>' to
      'const std::__1::function<int (int, int)> &' for 1st argument
    function(const function&);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:1629:5: note: candidate constructor not
      viable: no known conversion from '__bind<int (*)(Foo *, int, int), Foo *, const std::__1::placeholders::__ph<1> &>' to
      'std::__1::function<int (int, int)> &&' for 1st argument
    function(function&&) _NOEXCEPT;
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional:1631:5: note: candidate template
      ignored: requirement '__callable<__bind<int (*)(Foo *, int, int), Foo *, const __ph<1> &> >::value' was not satisfied [with _Fp =
      std::__1::__bind<int (*)(Foo *, int, int), Foo *, const std::__1::placeholders::__ph<1> &>]
    function(_Fp);
    ^
1 error generated.

这里有更多关于我要完成的任务的详细信息。m_callback 是我想要设置的。我可以将回调中需要的内容推送到 ClientData 中,但我喜欢 Command 结构来保存它的数据,而不必创建一个新结构作为每个命令的 ClientData 传递。

#include <functional>
#include <iostream>
#include <string>

typedef void (* callback) (Client* client, ClientData* client_data);

struct Command
{
    int execute() = 0;
}

struct Search : Command
{
    enum SearchType { kType1, kType2 };

    Search(Logger log, std::string query, SearchType type) : m_log(log), m_callback(), m_query(query), m_typ(type)
    {
        m_callback = // create callback
    }

    int execute(Client* client, ClientData* client_data)
    {
        client->query(client_data, m_callback, m_query, m_type);
    }

    static int my_callback(Foo* f, Client* client, ClientData* client_data);

    Logger& m_log;
    callback m_callback;
    std::string m_query;
    SearchType m_type;
    // other data I want in the callback that isn't passed in client_data
};

int main()
{
    Logger log;
    Search search(log, "some search", Search::kType1);
    Client client;
    ClientData client_data;

    search.execute(&client, client_data);
}

所以我想出了我做错了什么,std::bind但现在我需要将它转换为我需要使用的 C 样式回调。

艾哈迈德·米阿辛

bind 函数有问题,你只使用了 _1 但是你需要传递 2 个参数。

改变这个:

using std::placeholders::_1;
std::function<int(int,int)> new_func = std::bind(&Foo::foo_static, &f, _1);

using std::placeholders::_1;
using std::placeholders::_2;
std::function<int(int,int)> new_func = std::bind(&Foo::foo_static, &f, _1,_2);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章