【发布时间】:2011-04-11 13:52:51
【问题描述】:
这两行奇怪的代码是什么意思?
thread_guard(thread_guard const&) = delete;
thread_guard& operator=(thread_guard const&) = delete;
【问题讨论】:
标签: c++ c++11 deleted-functions
这两行奇怪的代码是什么意思?
thread_guard(thread_guard const&) = delete;
thread_guard& operator=(thread_guard const&) = delete;
【问题讨论】:
标签: c++ c++11 deleted-functions
=delete 是 C++0x 的一个新特性。这意味着一旦用户使用该函数,编译器应立即停止编译并抱怨“此函数已被删除”(另请参见:Bjarne Stroustrup 的 C++0x 常见问题解答中的defaulted and deleted functions -- control of defaults)。
thread_guard(thread_guard const&) 是一个复制构造函数,thread_guard& operator=(thread_guard const&) 是一个赋值构造函数。因此,这两行一起禁用 thread_guard 实例的复制。
【讨论】:
decltype 的表达式)可以被视为模板参数推导失败。这使得编译器只是忽略一个模板。它不会使编译器停止编译。
这是用于禁用类的某些功能的新 C++0x 语法。有关示例,请参阅wikipedia。在这里,您是在告诉 thread_guard 类既不可复制也不可分配。
【讨论】: