Skip to content
Justice Tech School

std::function

std::function stores any callable and is handy for callbacks.

Example
#include <iostream>
#include <functional>
#include <vector>

int main() {
  std::function<int(int)> f = [](int x) { return x * 2; };
  std::cout << f(5) << "\n";
  std::vector<std::function<int(int)>> fs = {f, [](int x) { return x + 1; }};
  for (auto &g : fs) std::cout << g(3) << " ";
  std::cout << "\n";
  return 0;
}

Result

10
6 4 

Exercise

Callable wrapper.

std::<int(int)>

Quiz Test

What does std::function hold?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.