+ 3

std::function

Is std::function an alternative for the normal way of writing a function(if it is, which is better?) . I've seen it on Google but got me confused 😕

3rd Feb 2021, 12:13 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
5 Respuestas
+ 8
std::function lets you use a function like a variable, an alternative to function pointers. It is in some cases the only way to store a lambda. std::function is not a function, but a way to store an existed function.
3rd Feb 2021, 12:19 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
//So it will be an alt for this? #include <iostream> using namespace std; int add(int a, int b){ return a + b; } int main() { int(*funct_ptr)(int,int); funct_ptr = add; int sum = funct_ptr(5,5); cout << sum; return 0;
3rd Feb 2021, 12:33 PM
Maher Al Dayekh
Maher Al Dayekh - avatar
+ 1
Yes
3rd Feb 2021, 1:48 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
ChillPill thanks it worked perfectly! Note that <functional> should be included. Also is there any reason to use it instead of function pointer? Thanks again for all <3
5th Feb 2021, 9:40 AM
Maher Al Dayekh
Maher Al Dayekh - avatar