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
10 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;
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
+ 1
Ketan Lalcheta I think it's easier to understand what is happening if you replaced bind with lambda https://sololearn.com/compiler-playground/c258W0YPzsPK/?ref=app
4th Mar 2025, 1:46 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li , thanks but why it works for bind asking for return type as void and we provided bool function
4th Mar 2025, 2:37 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Void as input argument is ok as we already bounded all required argument to bind.
4th Mar 2025, 2:37 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
0
Thanks Bob_Li I was trying to simplify my concern with some code example so I can share sample code and ask query: While preparing same, It worked actually but now I wonder why it works. Basically , mytaskexecutor is the concern where data type is void and return type is also void. I am wondering why isgreater bind successuly . It should not as it is having return type as void. https://sololearn.com/compiler-playground/clq4ZEb5vVHO/?ref=app
4th Mar 2025, 11:30 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
bind is quirky... now I'm wondering why any of your methods work at all... myTaskExecuter accepts argument with type signature of function<void(void)> which doesn't look like any of your class method's function signatures... 🤔 void(int,int) void(const vector<int>&) bool(int,int) maybe others can provide better insight into this, but bind seems to be turning them all to void() or void(void).... you also can't get the bool return value. from what I've been reading, bind is discouraged and lambda is the preferred substitute. https://www.reddit.com/r/cpp/s/7vjXxzvK1x https://abseil.io/tips/108
4th Mar 2025, 1:04 PM
Bob_Li
Bob_Li - avatar
0
Yeah
5th Mar 2025, 3:27 PM
Ram Charan Mondem
Ram Charan Mondem - avatar