std::bind,无法让具有单个参数的方法工作
std::bind, can't get method with single parameter to work
以下代码有效:
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();
相关文章:
- 如何将可变参数模板转换为多个单个模板?(C++竞争编程调试模板)
- 关于如何在具有单个参数的变体构造中选择替代方案?
- std::bind,无法让具有单个参数的方法工作
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 多个可变参数函数的单个模板参数包?
- 如何将多个函数重载作为单个参数传递?
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- C++fmt库,只使用格式说明符格式化单个参数
- 是否可以使用模板类的单个参数将类型和该类型的指针传递给 c++ 模板类?
- 如何使用每个对象的单个构造函数参数动态分配C++对象数组?
- 从单个标志C++获取多个参数
- QuickSort方法仅使用单个向量作为C 中的输入参数
- 模板参数在具有相同数据类型的单个类型名的构造函数中不起作用
- 具有单个参数的布尔函数映射
- 单个功能中的多个参数包
- 如何在C++中使用单个模板参数传递两个 lambda 函数
- 在什么基础上,由单个元素组成的参数包的折叠表达式被转换为未加括号的表达式
- 当foo是接受单个模板参数的结构时,'foo<Type1, Types...>'是否合法?
- C++是否可以创建依赖于单个构造函数参数的派生类而不是bass类
- C 模板字符在单个tick中作为参数