The code I\'m looking for is like following.
bool Func1(int Arg1, C++11LambdaFunc Arg2){
if(Arg1 > 0){
return Arg2(Arg1);
}
}
First solution:
You can make your Func1() function a function template:
template
bool Func1(int Arg1, T&& Arg2){
if(Arg1 > 0){
return Arg2(Arg1);
}
return false; // <== DO NOT FORGET A return STATEMENT IN A VALUE-RETURNING
// FUNCTION, OR YOU WILL GET UNDEFINED BEHAVIOR IF FLOWING
// OFF THE END OF THE FUNCTION WITHOUT RETURNING ANYTHING
}
You could then invoke it as you desire:
int main()
{
Func1(12, [](int D) -> bool { return D < 0; } );
}
Second solution:
If you do not want to use templates, an alternative (that would bring some run-time overhead) is to use std::function:
#include
bool Func1(int Arg1, std::function Arg2){
if(Arg1 > 0){
return Arg2(Arg1);
}
return false;
}
Once again, this would allow you to call Func1() the way you desire:
int main()
{
Func1(12, [](int D) -> bool { return D < 0; } );
}