Tuesday, August 22, 2017

C++11: function

The template std::function was added. It allows you to wrap functions. Here is an example:

#include <functional>
#include <iostream>

int add(int lhs, int rhs) {return lhs + rhs;}

int main()
{
  std::function<int(int,int)> f_add = add;

  std::cout << f_add(1, 2) << std::endl;
  return 0;
}
// Output: 3
Reference: http://en.cppreference.com/w/cpp/utility/functional/function

No comments:

Post a Comment