C++ 17现在功能齐全,因此不太可能经历大的变化.为C++ 17提出了数百个提案.
在C++ 17中,哪些特性被添加到C++中?
当使用支持"C++ 1z"的C++编译器时,当编译器更新到C++ 17时,哪些功能可用?
例如,stdlibc ++具有以下内容:
unique_lock& operator=(unique_lock&& __u)
{
if(_M_owns)
unlock();
unique_lock(std::move(__u)).swap(*this);
__u._M_device = 0;
__u._M_owns = false;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
为什么不直接将两个__成员分配给*?交换是否意味着__u被分配了*this成员,后来才分配0和false ...在这种情况下交换正在做不必要的工作.我错过了什么?(unique_lock :: swap只对每个成员执行std :: swap)
我发现std::condition_variable由于虚假的唤醒很难使用.所以有时我需要设置一个标志,如:
atomic<bool> is_ready;
Run Code Online (Sandbox Code Playgroud)
我在调用notify(或)之前设置is_ready为,然后我等待:truenotify_one()notify_all()
some_condition_variable.wait(some_unique_lock, [&is_ready]{
return bool(is_ready);
});
Run Code Online (Sandbox Code Playgroud)
有什么理由我不应该这样做:(编辑:好的,这真是一个坏主意.)
while(!is_ready) {
this_thread::wait_for(some_duration); //Edit: changed from this_thread::yield();
}
Run Code Online (Sandbox Code Playgroud)
如果condition_variable选择了等待时间(我不知道这是否属实),我宁愿自己选择.