我想知道为什么我不能在C++ 14(或17)中做到这一点
std::shared_timed_mutex mutex;
std::unique_lock<std::shared_timed_mutex> lock(mutex);
std::condition_variable var;
while(!some_condition)
var.wait(lock);
Run Code Online (Sandbox Code Playgroud)
条件变量似乎只适用于std :: mutex.但为什么?
请考虑以下C++代码.
struct foo { std::string value; }
inline foo bar() { return { "42" }; }
Run Code Online (Sandbox Code Playgroud)
现在假设我有一个以下列方式使用bar()的函数.
std::string my_func()
{
const auto &x = bar();
return x.value;
}
Run Code Online (Sandbox Code Playgroud)
这是否泄漏内存因为my_func只保存对x的引用?或者,在my_func终止后,x仍然会被清除吗?
我知道这不是应该如何使用引用.但我刚刚意识到这个编译很好,并想知道它的语义是什么.