std::bind to std::function?

22,298
std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1);
Share:
22,298

Related videos on Youtube

Philipp H.
Author by

Philipp H.

Updated on November 07, 2020

Comments

  • Philipp H.
    Philipp H. over 3 years

    I get a compile error using this:

    std::vector<std::function<int(int)>> functions;
    
    std::function<int(int, int)> foo = [](int a, int b){ return a + b; };
    std::function<int(int)> bar = std::bind(foo, 2);
    
    functions.push_back(bar);
    

    The error is:

    /usr/include/c++/4.6/functional:1764:40: error: no match for call to '(std::_Bind(int)>) (int)'

    Can someone tell me how to convert a std::bind into a std::function?

  • BlackMamba
    BlackMamba almost 8 years
    ` std::bind(foo, 2, std::placeholders::_1)` is equal to ` std::bind(foo, 2,)` ?
  • Caleth
    Caleth over 6 years
    @BlackMamba I think you are getting confused about which function has 2 parameters. A placeholder other than _1 implies the bind result has more than 1 parameter
  • Francis Cugler
    Francis Cugler over 5 years
    This is the second Q/A I've came across this week that where the provided answer had used std::placeholders::... and before this week I was not familiar with it. It's always good to learn something new.