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.
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;
+ 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
+ 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
+ 1
Bob_Li , thanks but why it works for bind asking for return type as void and we provided bool function
+ 1
Void as input argument is ok as we already bounded all required argument to bind.
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 ?
0
how about auto?
https://sololearn.com/compiler-playground/c2dqYdi320gT/?ref=app
you can also try std::variant
https://sololearn.com/compiler-playground/cHe5I8Kqw4bt/?ref=app
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
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
0
Yeah