以下代码有效:
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);
但是如果我想绑定(bind)到一个只有一个参数的方法:
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 (Foo::*)(int)
绑定(bind)到带有签名 void (*)(int)
的可调用对象,您必须编写
auto f = std::bind(&Foo::print, &foo, _1);
请注意,在 C++11(和一些 C++03 编译器,例如 Microsoft VS2010+)和更高版本中,您可以使用 lambda 表达式来执行相同的操作。
关于c++ - std::bind,无法使具有单个参数的方法起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52328037/