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
0
votes
0 answers

How to check if method is deleted and branch based on that?

My program has a meta object that takes a strategy at compile time. The strategy may either implement the method begin() or have it marked as delete'd. In the implementation, I now want to branch on exactly this condition and roll my own…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
0 answers

QT QML QAbstractListModel deleted function error

I am trying to pass a c++ user model to qml and get an error that I don't understand. I use a manager class that should reads in the users and fills the listmodel. The list model itself should be pass to qml via Q_PROPERTY. The manager class is…
user2377283
  • 365
  • 1
  • 2
  • 12
0
votes
0 answers

Explicitly deleted functions

we are migrating code to VS 2019 from legacy code We have an overloaded function on operator '<<' that is invoking basic_ostream function when executing the following lines. CStringArray asLine; using ostream = basic_ostream
Madhu Rao
  • 1
  • 1
0
votes
1 answer

Deriving from a class with a deleted destructor?

I am using CRTP to create a counter class, similar to Object Counter Additionally, classes that derive from this counter also should not be destructible. It will look something like this template class…
Nathan29006781
  • 173
  • 1
  • 9
0
votes
1 answer

Initializing unique_ptr causes an "error: use of deleted function" even though it's "std::move"ed

I am writing code that passes a std::unique_ptr through a few layers that look bad, but I don't have a choice but pass it all along for now. The problem is that I am getting an error when I try to pass the std::unique_ptr to a constructor of the…
0
votes
2 answers

Why can't the map be initialized?

Given the following code, #include #include #include #include #include struct sstruct { std::string content; std::string_view name; std::mutex mtx; sstruct(std::string…
0
votes
1 answer

Compiler error on call to recursive function - "attempting to reference a deleted function"

I'm trying to write a binary tree to a file using a recursive function. The call to writeFile(std::string fileName) opens the file stream and then passes it to the recursive function call, writeFile(BinNode ptr, std::ofstream outFile). The…
Kurt
  • 3
  • 3
0
votes
1 answer

Add class containing a circular buffer to Vector

I am trying to create a vector filled with class objects, and the class objects contain circular buffers as one of their members. I am running into this error: In file included from .pio/libdeps/teensy40/Vector/src/Vector.h:95:0, …
0
votes
0 answers

Use of deleted function

Hello newbie programmer here. Please help me. What should I do? #include #include using namespace std; class Konut { public: int yas; int metrekare; int kat; string isitma_turu; Konut(int a, int b, int c,…
Gökhan
  • 19
  • 2
0
votes
0 answers

axios delete operation with use reducer in react js

i am working on crud operation with context api and use reducer. i fetch data from an api and store it as initial value. but now i am confused how to delete a user from my fetched list. i made a remove function which works fine on manual refresh but…
0
votes
1 answer

Attempting to reference Deleted function shared_ptr

i have the following code inline entity_ptr Parser::parse(const std::string& json_str) { // std::cout << "parse: " << json_str << "\n"; entity_ptr entity = do_parse(json_str); if (entity && entity->is_notification()) { …
Zakkar
  • 3
  • 1
0
votes
1 answer

How do I assign an object containing an unique_ptr to a vector of its type when a move assignment operator is defined?

The self-contained program section below leads to this error on Visual Studio 2019: "function "partition_data::operator=(const partition_data &)" (implicitly declared)" is a deleted function. Based on research around this type of problem I have…
0
votes
1 answer

Conditions to declare function deleted by c++

ALL, What are the conditions where the compiler itself declares the function deleted? Consider following: class Foo { public: Foo(); virtual void func1() = 0; virtual void func2() = 0; virtual bool func3(); } class Bar : public…
Igor
  • 5,620
  • 11
  • 51
  • 103
0
votes
1 answer

Trying to locate a deleted function

Messing around in C++ for class and came across a error stating that i was trying to reference a deleted function. Here is the error C2280(Test &Test::operator = (const Test& : attempting to reference a deleted function). Here is my code: #include…
0
votes
0 answers

Why does this temporary created with deleted constructor compile?

This code: struct F { F() = delete; void foo() {}; }; int main() { F{}.foo(); } Live demo here. Suffice to say I would assume it to not compile (as we're calling a deleted constructor). Why does it compile on any compiler I've…
rubenvb
  • 74,642
  • 33
  • 187
  • 332