class my_class
{
    ...
    my_class(my_class const &) = delete;
    ...
};

在这种情况下= delete是什么意思?

是否有其他“修饰符”(除了= 0和= delete)?


当前回答

我使用过的编码标准对大多数类声明都有如下规定。

//  coding standard: disallow when not used
T(void)                  = delete; // default ctor    (1)
~T(void)                 = delete; // default dtor    (2)
T(const T&)              = delete; // copy ctor       (3)
T(const T&&)             = delete; // move ctor       (4)
T& operator= (const T&)  = delete; // copy assignment (5)
T& operator= (const T&&) = delete; // move assignment (6)

如果使用这6种方法中的任何一种,只需注释掉相应的行。

示例:类FizzBus只需要dtor,因此不使用其他5。

//  coding standard: disallow when not used
FizzBuzz(void)                         = delete; // default ctor (1)
// ~FizzBuzz(void);                              // dtor         (2)
FizzBuzz(const FizzBuzz&)              = delete; // copy ctor    (3)
FizzBuzz& operator= (const FizzBuzz&)  = delete; // copy assig   (4)
FizzBuzz(const FizzBuzz&&)             = delete; // move ctor    (5)
FizzBuzz& operator= (const FizzBuzz&&) = delete; // move assign  (6)

我们在这里只注释掉1,并在其他地方安装它的实现(可能是在编码标准建议的地方)。其他5个(6个中的5个)是不允许使用delete的。

你也可以使用'= delete'来禁止不同大小的隐式提升…例子

// disallow implicit promotions 
template <class T> operator T(void)              = delete;
template <class T> Vuint64& operator=  (const T) = delete;
template <class T> Vuint64& operator|= (const T) = delete;
template <class T> Vuint64& operator&= (const T) = delete;

其他回答

是否有其他“修饰符”(除了= 0和= delete)?

因为似乎没有其他人回答这个问题,我应该提到还有=default。

https://learn.microsoft.com/en-us/cpp/cpp/explicitly-defaulted-and-deleted-functions#explicitly-defaulted-functions

本文节选自The c++ Programming Language[第4版]- Bjarne Stroustrup一书,讲述了使用=delete的真正目的:

3.3.4 Suppressing Operations Using the default copy or move for a class in a hierarchy is typically a disaster: given only a pointer to a base, we simply don’t know what members the derived class has, so we can’t know how to copy them. So, the best thing to do is usually to delete the default copy and move operations, that is, to eliminate the default definitions of those two operations: class Shape { public: Shape(const Shape&) =delete; // no copy operations Shape& operator=(const Shape&) =delete; Shape(Shape&&) =delete; // no move operations Shape& operator=(Shape&&) =delete; ˜Shape(); // ... }; Now an attempt to copy a Shape will be caught by the compiler. The =delete mechanism is general, that is, it can be used to suppress any operation

一个小例子总结一些常见的用法:

class MyClass
{
public:
    // Delete copy constructor:
    // delete the copy constructor so you cannot copy-construct an object
    // of this class from a different object of this class
    MyClass(const MyClass&) = delete;

    // Delete assignment operator:
    // delete the `=` operator (`operator=()` class method) to disable copying
    // an object of this class
    MyClass& operator=(const MyClass&) = delete;

    // Delete constructor with certain types you'd like to
    // disallow:
    // (Arbitrary example) don't allow constructing from an `int` type. Expect
    // `uint64_t` instead.
    MyClass(uint64_t);
    MyClass(int) = delete;

    // "Pure virtual" function:
    // `= 0` makes this is a "pure virtual" method which *must* be overridden 
    // by a child class
    uint32_t getVal() = 0;
}

快乐吗?

我仍然需要做一个更彻底的例子,并运行它来显示一些用法和输出,以及它们对应的错误消息。

另请参阅

https://www.stroustrup.com/C++11FAQ.html#default - section“默认值控制:默认和删除”

删除函数是c++ 11的一个特性:

“禁止复制”这句俗语现在可以表达出来了 直接: 类X { / /…… X& operator=(const X&) = delete;//禁止复制 X(const X&) = delete; };

[…] “删除”机制可用于任何功能。例如,我们 可以像这样消除不希望的转换: 结构Z { / /…… Z(很久);//可以初始化一个long long Z(long) = delete;//但不能少 };

这是c++ 0x标准中的新功能,可以删除继承的函数。