Is returning a reference ever a good idea?
We all know that returning a reference to a local variable is a bad idea . However, I'm wondering if it's ever really a good idea to a return a reference at all and if it's possible to determine some good rules about when or when not to do it. My problem with returning a reference is that the calling function needs to care about the lifetime of an object that shouldn't be its responsibility. As a contrived example: #include <vector> const int& foo() { std::vector<int> v = {1, 2, 3, 4, 5}; return v[0]; } int main(int argc, const char* argv[]) { const int& not_valid = foo(); return 0; } Here,