How to pass function as an argument to another function in c++?

I need to pass function with no arguments to another function, how can I do it?

I googled a bit, and found out that I need to use std::function from functional, but didn’t understand how to pass function without arguments.

>Solution :

Using std::function you can achieve that like in the following example: c++ shell

#include <iostream>
#include <functional>

void myFunction() {
  std::cout << "Hello world!\n";

void foo(std::function<void()> f) {

int main() {
  foo(myFunction); // Pass the function without arguments
  return 0;

Leave a Reply