简体   繁体   中英

How to write a generic function that takes a lambda with a default argument?

I want to store a function in a variable. I have a generic setter function to assign it. If no function is specified I want to use a default function. How do I accomplish this?

My code so far:

void(*fmain)();

template <typename Func>
void setFunc(Func f = [](){cout << "Working!" << endl;}){
    fmain = f;
}

int main(){
    setFunc();

    fmain();
}

Supply both default template parameter and (matching) default function parameter:

void default_action(void) {cout << "Working!" << endl;};

template <typename Func = void ( * )(void)>
void setFunc(Func f = &default_action){
    fmain = f;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM