将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()却不是。