Questions tagged [deleted-functions]

C++11 introduced the ability to mark member functions as deleted, which means that any attempt to call those functions causes a compilation error. This can be used to prevent improper usage of a class. For example, if a class is meant to manage a unique resource, an object of that class shouldn't be copiable. This can be achieved by deleting its copy constructor and copy assignment functions.

With C++11 the delete keyword can be used to explicitly forbid the usage of a member function: if it is deleted, any attempt to call it will cause a compilation error (there is no difference at runtime). This is useful when calling that function could lead to wrong results and/or unexpected behaviour.

An example is a class that has to manage a unique resource: an object of that class must not be copiable. Normally, to ensure that a function is not used, a programmer must simply avoid declaring it. But in some cases it is not enough, because some functions can be automatically generated by the compiler even though the programmer hasn't declared them. These functions include constructors, copy/move constructors and copy/move assignments. Not defining them doesn't guarantee that they can't be used. The solution is to use the = delete; syntax.

For example, the following class declares a constructor and a destructor, but deletes the copy constructor and copy assignment:

class Unique_manager {
    Unique_manager();                                          // Constructor
    ~Unique_manager();                                         // Destructor
    Unique_manager(const Unique_manager&)            = delete; // Forbid copy constructor
    Unique_manager& operator=(const Unique_manager&) = delete; // Forbid copy assignment
};

In this case it would be possible to create an object of this class:

Unique_manager db_access_manager;

but an attempt to create a copy from it would be blocked by the compiler:

Unique_manager another_db_manager(db_access_manager);  // Error: copy constructor
Unique_manager one_more_access_manager;                // OK:    constructor
one_more_access_manager = db_access_manager;           // Error: copy assignment
104 questions
244
votes
3 answers

How is "=default" different from "{}" for default constructor and destructor?

I originally posted this as a question only about destructors, but now I'm adding consideration of the default constructor. Here's the original question: If I want to give my class a destructor that is virtual, but is otherwise the same as what…
167
votes
7 answers

error: use of deleted function

I've been working on some C++ code that a friend has written and I get the following error that I have never seen before when compiling with gcc4.6: error: use of deleted function ‘GameFSM_ >::hdealt::hdealt()’ is implicitly…
shuttle87
  • 15,466
  • 11
  • 77
  • 106
43
votes
2 answers

Can one delete a function returning an incomplete type in C++?

In the following example function f() returning incomplete type A is marked as deleted: struct A; A f() = delete; It is accepted by GCC, but not in Clang, which complains: error: incomplete result type 'A' in function definition Demo:…
Fedor
  • 17,146
  • 13
  • 40
  • 131
40
votes
5 answers

Deletion of copy-ctor & copy-assignment - public, private or protected?

In order to make an object non-copiable we can explicitly delete both its copy-constructor and copy-assignment operator. My question is: What is the right place to do it - in the public, private or protected section of the class? And - does this…
Sajal
  • 1,783
  • 1
  • 17
  • 21
34
votes
3 answers

Default move constructor/assignment and deleted copy constructor/assignment

According to the standard, If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if — X does not have a user-declared copy constructor, — X does not have a…
P-Gn
  • 23,115
  • 9
  • 87
  • 104
23
votes
2 answers

Deleting copy constructors and copy assignment operators. Which of them are essential?

I have a use case that my object must not be copied in any way. I have written an exaggerated complete list of copy constructor and copy assignment operator deletions below. There are so many of them that I can't make sure which ones to use, and…
21
votes
2 answers

Deleting virtual functions in C++0x

It isn't clear what happens if I delete a virtual method in C++0x: virtual int derive_func() = delete; Does this mean this class and everything that inherits from it can not define/implement the derive_func() method? Or is this illegal/compile…
user34537
19
votes
2 answers

Class with all automatically-generated constructors/operators deleted can still be returned from a function?

Recently, I came across this answer which describes how to initialize a std::array of non-default-constructible elements. I was not so surprised because that answer clearly doesn't do any default-constructing. Instead, it is constructing a…
Bernard
  • 5,209
  • 1
  • 34
  • 64
19
votes
1 answer

Specialized template function with deleted "general" case fails to compile with g++ <=4.8.0 and clang++

Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile: template void foo() = delete; template<> void foo(){} int main() { foo(); return 0; } It seems that g++ doesn't…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
17
votes
6 answers

Are there any use cases for a class which is copyable but not movable?

After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable, I starting wondering if there are use cases for a class which can be copied but not moved. Technically, this is possible: struct…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
14
votes
2 answers

std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

I have a class with a deleted move constructor and when I try to call std::vector::push_back() in MSVC (v.15.8.7 Visual C++ 2017) I get an error saying that I am trying to access the deleted move constructor. If however I define the move…
Brent
  • 143
  • 3
14
votes
1 answer

Is there any point in declaring a deleted function as noexcept?

Consider these two possible definitions for a class: Exhibit A: struct A { A() = delete; }; Exhibit A′: struct A { A() noexcept = delete; } Is there any point in declaring a deleted function as noexcept?
user2296177
  • 2,807
  • 1
  • 15
  • 26
10
votes
3 answers

error C2280: attempting to reference a deleted function

I'm new to game development and very new to c++, but I've started developing a little Arkanoid game. I've had it running previously, but after refactoring (introducing the ArkanoidGame class) it doesnt compile and I cannot figure out why. The error…
Linora
  • 10,418
  • 9
  • 38
  • 49
9
votes
3 answers

Is deleting copy and move constructors/assignment operators in base class enough?

If I have an abstract base class and I want to make all derived classes noncopyable and nonmovable is it sufficient to declare these special member functions deleted in the base class? I want to ensure that my entire class hierarchy is noncopyable…
Mike Sweeney
  • 1,896
  • 2
  • 18
  • 20
9
votes
1 answer

Deleted copy constructor results in deleted default constructor

This code will not compile with gcc 4.7.0: class Base { public: Base(const Base&) = delete; }; class Derived : Base { public: Derived(int i) : m_i(i) {} int m_i; }; The error is: c.cpp: In constructor…
kounoupis
  • 329
  • 1
  • 10
1
2 3 4 5 6 7