The corresponding functionality in C++ is available through function objects std::function
:
void call(function<void()> f) {
f();
}
The argument that you pass could be a named function, e.g.
void test() {
cout << "test" << endl;
}
...
call(test);
or a lambda function, e.g.
call([]() { cout << "lambda" << endl; });
Demo.
I would like to do this in C++ with a function from the WinAPI.
Another way of passing a function is with function pointers, which is the approach used by WinAPI. You cannot do it inline, because the API does not take std::function
, so you have to declare a named function for use in the call:
BOOL CALLBACK CheckWindow(HWND hwnd, LPARAM lParam) {
... // Your processing code goes here
return TRUE;
}
...
EnumChildWindows(g_hWnd, CheckWindow, NULL);