代码之家  ›  专栏  ›  技术社区  ›  fatdragon

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

c++
  •  0
  • fatdragon  · 技术社区  · 4 年前

    以下代码无法编译:

    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 要成为一个全局函数,它就会起作用。

    为什么?

    1 回复  |  直到 4 年前
        1
  •  5
  •   HolyBlackCat    4 年前

    拆下 & . 即使指向函数的指针是可调用的,指向具有重载 operator() 他们不是。