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
8
votes
1 answer

Initialisation of aggregate with default constructor deleted in c++20

There is a struct containing POD and default constructor deleted. Trying to aggregate-initalize an instance of the struct results in compilation error in g++9.1 when compiled with -std=c++2a. The same code compiles fine with…
8
votes
2 answers

What is "= delete"?

What do these two strange lines of code mean? thread_guard(thread_guard const&) = delete; thread_guard& operator=(thread_guard const&) = delete;
Liu
  • 645
  • 2
  • 8
  • 16
7
votes
4 answers

How to in-place initialize an array?

How can I initialize an array without copy or move-constructing temporary elements? When the element has an explicitly deleted copy or move constructor, I can initialize the array only if the element has a default ctor or a ctor with all default…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
7
votes
1 answer

Overload rules for multiple, templated constructors in list initialization

I'm unsure if the following code is valid according to the c++11 standard and should have the same behavior across different implementations or not: #include struct Foo{ template constexpr Foo( const char ( &other…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
7
votes
1 answer

Error using defaulted copy constructor: "deleted function"

I am using g++ 5.1.0 to compile the following C++14 program test.cpp: #include class Factor { public: Factor(const Factor&) = default; Factor(Factor&&) = default; Factor& operator=(const Factor&) = default; Factor&…
Jake
  • 7,565
  • 6
  • 55
  • 68
7
votes
1 answer

Why is the copy assignment not deleted when the move assignment is declared?

struct A { A(int x) : n(x) {} A(A&&) {} A& operator =(A&&) { return *this; } int n; }; int main() { A a(1), b(2); a = b; if (2 == a.n) { // It SHOULD go here! } } As…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
6
votes
2 answers

C++ deleted constructors

Say I have this structure: struct F { int& ref; // reference member const int c; // const member // F::F() is implicitly defined as deleted }; That is from cppreference. As I understand from the documentation the constructor of F is…
Cantaff0rd
  • 705
  • 1
  • 6
  • 14
6
votes
4 answers

Can you swap a std::queue with a lambda comparator?

I am trying to clear a std::queue using the example in https://stackoverflow.com/a/709161/837451 via a swap. However, it doesn't seem to work with a lambda comparator due to the "deleted function" error. Minimal working failing example: #include…
mmdanziger
  • 4,466
  • 2
  • 31
  • 47
5
votes
1 answer

Is this non-copyable map legal c++11? GCC 4.7 and MSVS 2010 allow it. Clang 3.1 does not

I have created a non-copyable map which I cannot get to compile with clang. Since clang is meant to be very standards compliant I was wondering if my code was legal. MSVS 2010 and GCC 4.7 compile this code without warnings or errors. Full code is…
jbcoe
  • 3,611
  • 1
  • 30
  • 45
5
votes
1 answer

Detect deleted function

Is there a way to detect deleted functions after overload selection (over no viable or ambiguous overloads)? void foo(); void foo(double) = delete; void foo(std::string); void foo(char, int); void foo(int, char); static_assert(!foo_is_deleted<>); …
Jarod42
  • 203,559
  • 14
  • 181
  • 302
5
votes
0 answers

Finding specific function calls by means of C++ compiler

I have a big codebase and would like to find calls to specific standard or third-party library functions. If the names of the functions are not very unique and can be present also as names of variables and inside comments, it is hard to achieve…
Fedor
  • 17,146
  • 13
  • 40
  • 131
5
votes
2 answers

Would a derived class ever have an implicit copy constructor or assignment operator when it's deleted in the base class?

Qt defines Q_DISABLE_COPY as follows: #define Q_DISABLE_COPY(Class) \ Class(const Class &) = delete;\ Class &operator=(const Class &) = delete; Q_DISABLE_COPY is used in the QObject class, but the documentation for it says that it should be…
5
votes
3 answers

Can any function be a deleted-function?

The working draft explicitly calls out that defaulted-functions must be special member functions (eg copy-constructor, default-constructor, etc, (§8.4.2.1-1)). Which makes perfect sense. However, I don't see any such restriction on…
deft_code
  • 57,255
  • 29
  • 141
  • 224
5
votes
1 answer

Danger with virtual base move assignment operators when they are now allowed to be used?

This concerns the resolution of C++ Issue http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1402 . Summary: template struct wrap { wrap() = default; wrap(wrap&&) = default; wrap(const wrap&) = default; T…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
2 answers

C++ and ExprTk parser "use of deleted function" error

I'm trying to use the ExprTk mathematical expression parser library within a class whose objects are to be stored in a vector of objects, which is a member variable of another class; however, when I try to push_back a new object in the vector I'm…
mrmudd
  • 61
  • 5