gpt4 book ai didi

c++ - 如何在 std::bind 中指示给定签名的重载函数?

转载 作者:可可西里 更新时间:2023-11-01 18:04:20 25 4
gpt4 key购买 nike

我有 api 函数 f_api(std::function<void(int)> func)现在我有了我的过程类

class Func {
public:
void operator()(int i) {
// do some work
}
int operator()(int i, int j) {
// do some other work
}
};

我想使用 Func f f(int i) 传递给 f_api 来完成工作;所以我使用 std::bind

Func f;
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
f_api(func);

但这里的问题是,我如何指示哪个 Func::operator()()我要绑定(bind)?我可以通过它的名字给一个成员函数,但是当这个成员函数有几个不同的签名重载函数时我该如何处理呢? std::bind 会为我找到最适合调用的函数吗? C++好复杂.....

最小可验证案例:

#include <iostream>
#include <functional>

using namespace std;

class Func {
public:
void operator()(int i) {
// do some work
cout << "i is: " << i << endl;
}
int operator()(int i, int j) {
// do some other work
}
};

void f_api(function<void(int)> f) {
f(3);
}

int main () {
Func f;
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
f_api(func);
return 0;
}

编译错误:

a.cpp: In function ‘int main()’:
a.cpp:23:91: error: no matching function for call to ‘bind(<unresolved overloaded function type>, Func*, const std::_Placeholder<1>&)’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^
a.cpp:23:91: note: candidates are:
In file included from a.cpp:2:0:
/usr/include/c++/4.8/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1655:5: note: template argument deduction/substitution failed:
a.cpp:23:91: note: couldn't deduce template parameter ‘_Func’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^
In file included from a.cpp:2:0:
/usr/include/c++/4.8/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/4.8/functional:1682:5: note: template argument deduction/substitution failed:
a.cpp:23:91: note: couldn't deduce template parameter ‘_Result’
std::function<void(int)> func = std::bind(&Func::operator(), &f, std::placeholders::_1);
^

在我的情况下有点不同,因为我的 class Func无法分配,因为该类的一个成员字段不可分配,所以在编译期间我会得到一个稍微不同的错误。

最佳答案

对于所有重载,您可以采用令人讨厌的漫长方法。通过类型转换:

using sig1 = void (Func::*)(int);
using sig2 = void (Func::*)(int, int);

std::bind(static_cast<sig1>(&Func::operator()), &f, std::placeholders::_1);

或者,如果您有 lambda,您可能会认识到 std::bind 并不是那么有用:

std::function<void(int)> func = [&](int i) { f(i); };

关于c++ - 如何在 std::bind 中指示给定签名的重载函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45482319/

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