The following compiles fine:
#include <functional>
int main()
{
std::function<const int&()> f = []() -> int {return 1;};
const int& r = f(); // r is a dangling reference
return 0;
}
How come it's possible to set an std::function
with a const int&
return type to a lambda with an int
return type? Allowing this sort of cast to happen implicitly and with no warning is a gotcha IMHO.