How to pass and execute anonymous function as parameter in C++11?

后端 未结 3 1794
时光取名叫无心
时光取名叫无心 2020-12-04 10:53

The code I\'m looking for is like following.

bool Func1(int Arg1, C++11LambdaFunc Arg2){
    if(Arg1 > 0){
        return Arg2(Arg1);
    }
}
3条回答
  •  春和景丽
    2020-12-04 11:16

    For those whose tastes are more traditional, note that non-capturing lambdas can convert to function pointers. So you can write your function above as:

    bool Func1(int Arg1, bool (*Arg2)(int)) { ... }
    

    And it will work correctly for both traditional functions and lambdas.

提交回复
热议问题