HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Meaning of `= delete` after function declaration

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
declarationdeletefunctionmeaningafter

Problem

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

Solution

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 a long long      
    Z(long) = delete; // but not anything less
};

Code Snippets

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

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

Context

Stack Overflow Q#5513881, score: 281

Revisions (0)

No revisions yet.