+ 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 😕
5 Antworten
+ 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.
+ 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;
+ 1
Yes
+ 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



