I have the following code:
auto get_functor = [&](const bool check) {
return [&](const foo& sr)->std::string {
if(check){
return "some string";
}
return "another string"
};
};
run(get_functor(true));
The run function signature:
void run(std::function<std::string(const foo&)> func);
I am getting the following error which is not so clear for me:
error C2440: 'return' : cannot convert from 'main::<lambda_6dfbb1b8dd2e41e1b6346f813f9f01b5>::()::<lambda_59843813fe4576d583c9b2877d7a35a7>' to 'std::string (__cdecl *)(const foo &)'
P.S. I am on MSVS 2013
Edit:
if I edit the code by replacing auto with the real type:
std::function<std::string(const foo&)> get_functor1 = [&](const bool check) {
return [&](const foo& sr)->std::string {
if (check) {
return "some string";
}
return "another string";
};
};
run(get_functor1(true));
I am getting another error:
error C2664: 'std::string std::_Func_class<_Ret,const foo &>::operator ()(const foo &) const' : cannot convert argument 1 from 'bool' to 'const foo &'
Which is totally messed up!
return [=](const foo& sr) .... Since here you are getting a reference that will be outlived. Then, what's the full code example that leads to that error?std::functionobject