Работает следующий код:
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