Meaning of = delete after function declaration

Pat O'Keefe picture Pat O'Keefe · Apr 1, 2011 · Viewed 114.5k times · Source
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)?

Answer

Prasoon Saurav picture Prasoon Saurav · Apr 1, 2011

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
};