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
1
vote
2 answers

C2280 error attempint to reference a deleted function

The function DataContainer.initial() triggers a C2280 error. After defining a move assignment operator it works. But I am not clear why in function initial_2() it works. The obvious difference is that data_a is a local variable and data_b is a class…
stevenhz
  • 57
  • 6
1
vote
1 answer

Does the return type of a deleted operator matter if the operator is deleted?

I am reading ‘C++ concurrency in action’, one page talks about copy-assignment operator =delete. I have googled about it (Deleting copy constructors and copy assignment operators. Which of them are essential?) and tried some code by myself. I want…
1
vote
1 answer

Deleted function error after using random lib in struct

I'm trying to write a simple struct around an std random number generator. According to the compiler I can initialize this class but when I try to use the function it gives the following error: Error C2280 'RandNormGen::RandNormGen(const…
zarro
  • 45
  • 4
1
vote
1 answer

Should I put deleted member function in uml class diagram?

Should I put deleted method or member function in an UML class diagram, i.e. for example for a class like this: class ProfilometerManager { int a = 6; public: ProfilometerManager(ProfilometerManager& other) = delete; //can not be cloneable …
Gameriker
  • 35
  • 7
1
vote
1 answer

Explicit instantiation of a deleted function template in C++

If a function template is marked as deleted, is it allowed to explicitly instantiate it as in the example: template int foo(T) = delete; template int foo(int); Clang and GCC allows it, while MSVC prints the error: error C2280: 'int…
Fedor
  • 17,146
  • 13
  • 40
  • 131
1
vote
3 answers

Error: Use of deleted function std::unique_ptr

I am trying to pass a unique_ptr into a custom vector class but I am receiving the error in the subject title. I understand that you cannot copy a unique_ptr and so I am trying to use std::move() when passing it, however that doesn't seem to solve…
1
vote
1 answer

C++ primer 5th edition: Synthesized Move constructor as deleted and copy-constructor

I'm on the last appendix of C++ primer 5th edition. (Solution part): Here is an example from there: Assume Y is a class that defines its own copy-constructor but not a move-constructor struct hasY{ hasY() = default; hasY(hasY&&) =…
1
vote
0 answers

How do I fix attempting to reference deleted function error in C++ sfml

this is main.cpp #include #include #include #include #include "class.cpp" using namespace sf; int main() { HWND hwnd = GetConsoleWindow(); ShowWindow(hwnd, 0); RenderWindow…
1
vote
2 answers

Why does copy-and-swap in a base class cause the copy-assignment operator to be implicitly deleted in the derived class?

Tested only in GCC and Clang, the presence of a pass-by-value copy assignment operator in the base class (useful when implementing the copy-and-swap (or copy-and-move) idiom) causes the copy assignment operator in the derived class to be implicitly…
1
vote
2 answers

Conversion operator vs deleted constructor

Please see the following code: struct X; struct Y { Y() {} Y(X&) = delete; }; struct X { X() {} operator Y() { return{}; } }; int main() { X x; static_cast(x); } Here, the Y's constructor taking an X is explicitly deleted,…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
1
vote
1 answer

illegal use of deleted function

I have a class A struct A { A() = delete; A(const A&) = default; A& operator=(const A&) = default; A(A&&) = default; A& operator=(A&&) = default; explicit A(int i) .... // a few explicit constructors } when I am trying…
apramc
  • 1,346
  • 1
  • 15
  • 30
1
vote
1 answer

std::ifstream, use of deleted function

I am trying to write a method which will get data from a .txt file. I need to call this method from other methods. I have a problem with passing arguments to methods. Library.h: #include #include #include #include…
msk
  • 141
  • 1
  • 3
  • 11
1
vote
1 answer

Does std::list template require a copy constructor (or equivalent) in its instance type?

I have a class, for which every instance is to be accounted for, creation and destruction regulated tightly. No random moves, copies, temporaries allowed - once created through a dedicated function, the instance can be "passed around" only through…
SF.
  • 13,549
  • 14
  • 71
  • 107
1
vote
1 answer

C++ non heap Factory object creation with protected constructor and copy constructor

Because of RAII features i want my objects to be placeable only on the stack and also as object creation should be delegated to specialised factories i dont want ocpy constructor to be accessible for use. So i did something like…
1
vote
1 answer

error: use of deleted function boost::filesystem3::directory_iterator

I have this strange error when I try to compile a C++ and qt project: error: use of deleted function ‘boost::filesystem3::directory_iterator::directory_iterator(const boost::filesystem3::directory_iterator&)’ There is no delete() used anywhere in…
user2402328
  • 11
  • 1
  • 2