2

Given my function that takes a function pointer as parameter

void call_func(std::function<void()>func) {
    func();
}

The most straight forward way is something like

void no_op() {
    ;
}

void call_func(std::function<void()>func = no_op) {
    func();
}

Is there a cleaner way to do this so that I can avoid creating the literally useless no_op function?

Rufus
  • 5,111
  • 4
  • 28
  • 45

1 Answers1

6

You can use an empty lambda, which could be also wrapped by std::function. e.g.

void call_func(std::function<void()>func = []{}) {
    func();
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405