Scenario:
I have a method which expects a std::function as parameter
typedef std::function<void(void)> Callback;
SomeMethod(Callback & call_back) {
std::thread t([call_back](){
call_back();
});
}
I call SomeMethod by passing a lambda into it. So, I am expecting a call back at some point of time.
void MyFunction() {
std::cout << "MyFunction starts" << std::endl;
SomeMethod([]() {
std::cout << "Callback received" << std::endl;
return; // Does this return apply only to lambda or to MyFunction?
});
std::cout << "MyFunction ends" << std::endl;
}
Question:
1. I want to understand what a return statement inside a lambda means. Does return statement inside a lambda exit the lambda or the calling method itself which is MyFunction in this case. Will calling return, cause an exit MyFunction?
2. Is it okay to call return inside lambdas?
Aucun commentaire:
Enregistrer un commentaire