27

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?

leiyc
  • 903
  • 11
  • 23
Philipp H.
  • 1,513
  • 3
  • 17
  • 31

1 Answers1

30
std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1);
inkooboo
  • 2,904
  • 1
  • 19
  • 24
  • ` std::bind(foo, 2, std::placeholders::_1)` is equal to ` std::bind(foo, 2,)` ? – BlackMamba Jul 26 '16 at 08:56
  • @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 – Caleth Aug 25 '17 at 09:42
  • 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. – Francis Cugler Nov 07 '18 at 08:36