将std::bind与lambda函数一起使用

以下代码无法编译:

auto greater_than = [](const int a, const int b) { return a > b; };
auto is_adult = bind(&greater_than, placeholders::_1, 17);
cout << "Age 19 is adult = " << is_adult(19) << endl;

is_adult呼叫是给一个模糊的错误:

error: no matching function for call to object of type 'std::_Bind<(lambda at main.cpp:62:23) *(std::_Placeholder<1>, int)>'

但是,如果我将其移动greater_than为全局函数,则它可以工作。

这是为什么?

回答

删除&. 尽管指向函数的指针是可调用的,但指向重载对象的指针operator()却不是。


以上是将std::bind与lambda函数一起使用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>