【问题标题】:What is "= delete"?什么是“=删除”?
【发布时间】:2011-04-11 13:52:51
【问题描述】:

这两行奇怪的代码是什么意思?

thread_guard(thread_guard const&) = delete;

thread_guard& operator=(thread_guard const&) = delete;

【问题讨论】:

    标签: c++ c++11 deleted-functions


    【解决方案1】:

    =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 的表达式)可以被视为模板参数推导失败。这使得编译器只是忽略一个模板。它不会使编译器停止编译。
    【解决方案2】:

    这是用于禁用类的某些功能的新 C++0x 语法。有关示例,请参阅wikipedia。在这里,您是在告诉 thread_guard 类既不可复制也不可分配。

    【讨论】:

      猜你喜欢
      • 2011-01-27
      • 2010-10-26
      • 2020-12-12
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多