【发布时间】:2015-11-05 21:25:26
【问题描述】:
考虑以下 C++ 代码。
struct foo { std::string value; }
inline foo bar() { return { "42" }; }
现在想象一下,我有一个以下列方式使用 bar() 的函数。
std::string my_func()
{
const auto &x = bar();
return x.value;
}
由于 my_func 只包含对 x 的引用,这是否会泄漏内存?还是在 my_func 终止后 x 仍然被清理?
我知道这不是引用的使用方式。但我刚刚意识到这编译得很好,想知道它的语义是什么。
【问题讨论】:
-
这应该编译吗?你不需要
const auto&吗? -
你是对的。我的原始代码中有 const auto&。