liveBook · Manning

Deleted function

As explained in 100 C++ Mistakes and How to Avoid Them

The C++ delete specifier marks a function or class member as unavailable so that attempts to provide or use it are diagnosed at compile time.

Use =delete to state that a function or class member is intentionally unavailable. Calls to deleted members become clear compile-time errors instead of obscure link-time failures. section 2.4 section 9.2

Preventing accidental copying

A unique ArtPiece can forbid both copy construction and copy assignment with `ArtPiece(const ArtPiece&) = delete;` and `ArtPiece& operator=(const ArtPiece&) = delete;`. Attempts such as `ArtPiece dup(ml);` or `ts = ml;` are diagnosed by the compiler. section 9.2

Listing 2.8 A simple class with defaulted and deleted members
class Container {
private:
    std::vector<int> values;
public:
    Container() = default;                                      #1
    Container(const Container& o) : values(o.values) {}
    Container& operator=(const Container& o) = delete;          #2
    void add(int n) { values.push_back(n); }
};
 
int main() {
    Container c1;
    c1.add(42);
    Container c2(c1);
    // c2 = c1;                                                 #3
    return 0;
}

Compare a Container with a defaulted constructor, a custom copy constructor, and a deleted assignment operator. section 2.4

Listing 9.6 Preventing duplication using delete=
class ArtPiece {
private:
    int id;
    std::string description;
public:
    ArtPiece(int id, std::string d) : id(id), description(d) {}
    ArtPiece(const ArtPiece&) = delete;
    ArtPiece& operator=(const ArtPiece&) = delete;
    const std::string& getDesc() const { return description; }
    //void badMethod() { ArtPiece a = *this; }
};
 
int main() {
    ArtPiece ml(333, "Mona Lisa");
    ArtPiece ts(444, "The Scream");
    // ArtPiece dup(ml);                                #1
    // ts = ml;                                         #2
    std::cout << ml.getDesc() << '\n';
    // ts.badMethod();                                  #3
    return 0;
}

Inspect a complete noncopyable ArtPiece and the commented operations that now produce compile-time errors. section 9.2

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains intentional omission and compile-time diagnostics, contrasting legacy private declarations with link-time failures.
Examples
Demonstrates Container assignment prevention and ArtPiece copy prevention through commented-out invalid operations.
What you can do after
Apply it by declaring unwanted members =delete and obtain direct compile-time diagnostics for invalid uses.
How it connects
Connects deleted members with copy constructors, copy assignment operators, default constructors, and compiler-generated operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.