Is there any differences?
Which is the best way to "save/transfer" function?
function<void(int)> fcn =
[](int par) {std::cout<<"fcn: "<<par<<std::endl; };
void(*fcn_a)(int) =
[](int par) {std::cout<<"fcn_a: "<<par<<std::endl; };
fcn(12);
fcn_a(12);
std::function
is more generic - you can store in it any callable object with correct signature (function pointer, method pointer, object with operator()
) and you can construct std::function
using std::bind.
Function pointer can only accept functions with correct signature but might be slightly faster and might generate slightly smaller code.