std::bind,无法让具有单个参数的方法工作

std::bind, can't get method with single parameter to work

本文关键字:参数 单个 方法 工作 bind std      更新时间:2023-10-16

以下代码有效:

struct Foo {
void print(int n1, int n2)
{
std::cout << n1 << " " << n2 << std::endl;
}
};
Foo foo;
auto f = std::bind(&Foo::print, &foo, 95, _1);
f(5);

但是,如果我想绑定到具有单个参数的方法:

struct Foo {
void print(int n1)
{
std::cout << n1 << std::endl;
}
};
Foo foo;
auto f3 = std::bind(&Foo::print, &foo, 95, _1);
f3();

我收到一长串错误:

g++ -Wall -O3 -fno-rtti -pedantic -Wextra -pthread -std=c++17 -g   -c -o main.o main.cpp
In file included from main.cpp:3:
/usr/include/c++/8/functional: In instantiation of 'struct std::_Bind_check_arity<void (Foo::*)(int), Foo*, int, const std::_Placeholder<1>&>':
/usr/include/c++/8/functional:787:12:   required from 'struct std::_Bind_helper<false, void (Foo::*)(int), Foo*, int, const std::_Placeholder<1>&>'
/usr/include/c++/8/functional:808:5:   required by substitution of 'template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (Foo::*)(int); _BoundArgs = {Foo*, int, const std::_Placeholder<1>&}]'
main.cpp:152:50:   required from here
/usr/include/c++/8/functional:775:7: error: static assertion failed: Wrong number of arguments for pointer-to-member
static_assert(_Varargs::value
~~~~~~~~~~~~~~~
? sizeof...(_BoundArgs) >= _Arity::value + 1
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: sizeof...(_BoundArgs) == _Arity::value + 1,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:153:8: error: no match for call to '(std::_Bind<void (Foo::*(Foo*, int, std::_Placeholder<1>))(int)>) ()'
f3();
^
In file included from main.cpp:3:
/usr/include/c++/8/functional:480:2: note: candidate: 'template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = void (Foo::*)(int); _Bound_args = {Foo*, int, std::_Placeholder<1>}]'
operator()(_Args&&... __args)
^~~~~~~~
/usr/include/c++/8/functional:480:2: note:   template argument deduction/substitution failed:
/usr/include/c++/8/functional:491:2: note: candidate: 'template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = void (Foo::*)(int); _Bound_args = {Foo*, int, std::_Placeholder<1>}]'
operator()(_Args&&... __args) const
^~~~~~~~
/usr/include/c++/8/functional:491:2: note:   template argument deduction/substitution failed:
/usr/include/c++/8/functional:509:2: note: candidate: 'template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (Foo::*)(int); _Bound_args = {Foo*, int, std::_Placeholder<1>}]'
operator()(_Args&&... __args) volatile
^~~~~~~~
/usr/include/c++/8/functional:509:2: note:   template argument deduction/substitution failed:
/usr/include/c++/8/functional:521:2: note: candidate: 'template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = void (Foo::*)(int); _Bound_args = {Foo*, int, std::_Placeholder<1>}]'
operator()(_Args&&... __args) const volatile

_1_2等是结果可调用对象的参数的表示。

例如
struct Foo {
void print(int n1)
{
std::cout << n1 << " " << n2 << std::endl;
}
};
Foo foo;
auto f = std::bind(&Foo::print, &foo, _2, _1);
f(5,4); //would call foo.print(4,5)

所以如果你需要用签名void (*)(int)void (Foo::*)(int)绑定到可调用的,你必须写

auto f = std::bind(&Foo::print, &foo, _1);

请注意,在 C++11(以及一些 C++03 编译器,例如 Microsoft VS2010+(及更高版本中,您可以使用 lambda 表达式来执行相同的操作。

您错误地复制粘贴了代码。试试这个:

struct Foo {
void print(int n1)
{
std::cout << n1 << std::endl;
}
};
Foo foo;
auto f3 = std::bind(&Foo::print, &foo, 95);
f3();