What is the fastest and shortest way to pass a function as parameter of another function without using other libraries other than the std one in just one line?
I mea
Try "pointers to member functions" if your function is a member of any class.
You are looking at several options.
std::function
which is part of C++ TR1 and C++11std::ptr_fun
to adapt your functionThe syntax you are showing is only going to work with C++11. Earlier C++ versions don't offer the possibility to define an anonymous function.
C++11 provides anonymous functions:
forloop(3, []{ std::cout "Hi!"; });
Example:
#include <iostream>
#include <functional>
void forloop(int times, std::function<void()> f) {
for(int i = 0; i < times; i++) {
f();
}
}
int main() {
forloop(3, [] () { std::cout << "Hello world"; });
}