gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:18:13 31 4
gpt4 key购买 nike

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

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

我好像什么都试过了。我以here为例并尝试使用类似的示例将它重构到我的用例中,但没有成功 here .

查看下面的更多最新示例

#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);

关于c++ - 如何使用 C++ 静态类方法创建 C 风格的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083939/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com