I noticed that lambdas both work using function pointers as well as the dedicated function
type using g++
.
#include <functional>
typedef int(*fptr)(int, int);
int apply_p(fptr f, int a, int b) {
return f(a, b);
}
int apply_f(std::function<int(int, int)> f, int a, int b) {
return f(a, b);
}
void example() {
auto add = [](int a, int b) -> int {
return a + b;
};
apply_p(add, 2, 3); // doesn't give an error like I'd expect it to
apply_f(add, 2, 3);
}
My question is: Which of these are most idiomatic to use? And what are the dangers and/or benefits of using one over the other?