0
Void functional void with bool function
Hi I have a function which has below as argument: Void std::function(void) If this is the case, how to pass a function pointer of function which returns bool and does not take argument.
3 Respostas
+ 2
You can use a lambda:
bool boolFunction() {
std::cout << "Running bool function\n";
return true;
}
int main() {
std::function<void()> func = []() {
boolFunction(); // Call the bool function, but remove return value
};
// Now call std::function(void)
func();
}
Or use a wrapper function instead:
void wrapper() {
bool result = boolFunction();
}
std::function<void()> func = wrapper;
+ 1
In other words, can I have something which accepts both:
std::function<void()> and std::function<bool()>
I actually had many argument to function also but that I am taking care by passing argument to std::find. How to handle return type of function so that bool function() and void function() is also allowed to get called
0
Unfortunately, I might be not able to do this. Additional wrapper (void function where we just ignore return type and this void function calls bool function)is not something I am looking for.
Is there anyway which makes it very generic in first place ?