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.

3rd Mar 2025, 5:16 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
3 odpowiedzi
+ 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;
3rd Mar 2025, 5:42 PM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
+ 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
3rd Mar 2025, 5:49 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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 ?
3rd Mar 2025, 5:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar