Meaning of = delete after function declaration

142,143

Solution 1

Deleting a function is a C++11 feature:

The common idiom of "prohibiting copying" can now be expressed directly:

class X {
    // ...
    X& operator=(const X&) = delete;  // Disallow copying
    X(const X&) = delete;
};

[...]

The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion like this:

struct Z {
    // ...

    Z(long long);     // can initialize with an long long         
    Z(long) = delete; // but not anything less
};

Solution 2

  1. = 0 means that a function is pure virtual and you cannot instantiate an object from this class. You need to derive from it and implement this method
  2. = delete means that the compiler will not generate those constructors for you. AFAIK this is only allowed on copy constructor and assignment operator. But I am not too good at the upcoming standard.

Solution 3

This excerpt from The C++ Programming Language [4th Edition] - Bjarne Stroustrup book talks about the real purpose behind using =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

Solution 4

Are there any other "modifiers" (other than = 0 and = delete)?

Since it appears no one else answered this question, I should mention that there is also =default.

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

Solution 5

The coding standards I've worked with have had the following for most of class declarations.

//  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)

If you use any of these 6, you simply comment out the corresponding line.

Example: class FizzBus require only dtor, and thus do not use the other 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)

We comment out only 1 here, and install the implementation of it else where (probably where the coding standard suggests). The other 5 (of 6) are disallowed with delete.

You can also use '= delete' to disallow implicit promotions of different sized values ... example

// 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;
Share:
142,143

Related videos on Youtube

Pat O'Keefe
Author by

Pat O'Keefe

Updated on September 12, 2020

Comments

  • Pat O'Keefe
    Pat O'Keefe over 3 years
    class my_class
    {
        ...
        my_class(my_class const &) = delete;
        ...
    };
    

    What does = delete mean in that context?

    Are there any other "modifiers" (other than = 0 and = delete)?

    • Blindy
      Blindy about 13 years
      I stand corrected, I had missed this C++0x feature. I was thinking it was a #define a la Qt that evaluated to a 0 and then declared a hidden function or something.
    • Stewart
      Stewart over 5 years
      I've a recollection of a 'disable' keyword which means the same or something similar. Am I imagining it? Or is there a subtle difference between them?
  • LiKao
    LiKao about 13 years
    There are some other uses of the =delete syntax. For example you can use it to explicitly disallow some kind of implicit conversions that might take place with the call. For this you just delete the overloaded functions. Have a look a the Wikipedia page on C++0x for more info.
  • UncleBens
    UncleBens about 13 years
    You can delete any function. E.g void foo(int); template <class T> void foo(T) = delete; stops all implicit conversions. Only arguments of int type are accepted, all others will try to instantiate a "deleted" function.
  • mkaes
    mkaes about 13 years
    I will do that as soon as I find some. Guess it it time to catch up with c++0X
  • LiKao
    LiKao about 13 years
    Yeah, C++0x rocks. I can't wait for GCC 4.5+ to be more common, so I can start using lambdas.
  • MicroVirus
    MicroVirus over 7 years
    The description for = delete is not entirely correct. = delete can be used for any function, in which case it is explicitly marked as deleted and any use results in a compiler error. For special member functions, this also means in particular that they then aren't generated for you by the compiler, but that is only a result of being deleted, and not what = delete truly is.
  • Reb.Cabin
    Reb.Cabin over 7 years
    Isn't the traditional method to "prohibit copying" just to make the copy-ctor and operator= "private?" This goes a bit further and instructs the compiler to not even generate the functions. If they're both private and =delete, is copying doubly-prohibited?
  • Toby Speight
    Toby Speight almost 7 years
    @Reb, =delete makes the method inaccessible even from contexts that can see private methods (i.e. within the class and its friends). This removes any uncertainty when you're reading the code. @Prasoon, that second example is still only deleting constructors - it would be nice to see a deleted operator long () for example.
  • Leushenko
    Leushenko about 5 years
    @Reb.Cabin Using = delete is better than using private or other similar mechanisms because usually you want the forbidden function to be visibly declared and considered for overload resolution etc., so that it can fail as early as possible and provide the clearest error to the user. Any solution which involves "hiding" the declaration reduces this effect.
  • Dohn Joe
    Dohn Joe almost 5 years
    Is there a special reason to make the copy constructor public and apply the delete keyword. Why not leave the constructor private and apply the keyword?
  • KeyC0de
    KeyC0de over 3 years
    Creating an object of a class with a deleted constructor is illegal.
  • 2785528
    2785528 over 3 years
    @Nikos - No -- You simply need to provide a constructor. The example of Adding " T() = delete; " stops the compiler from adding a (do minimal) default ctor, which is occasionally useful, but still allows you to add a (presumably-does-something-useful) ctor.
  • The Philomath
    The Philomath over 3 years
    Not always. You can't delete a base class virtual function in derived.
  • A Fog
    A Fog almost 2 years
    = delete also works with non-member functions. This is useful for preventing implicit type conversions.