C++ temporary variable lifetime
问题 Is this code valid? int foo() { std::vector<std::string>& v = std::vector<std::string>(5, "X"); // Do something silly... return 42; } For some reason I thought that the temporary std::vector object (right from the assignment sign) should be destructed right after it's construction (thus rendering the reference invalid) . However, debugging proves that I'm wrong and, well, I realized that I don't quite understand why does the temporary variable is destructed when the function returns. I guess