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
4
votes
2 answers

How to disable instantiating a temporary class?

I'm working with an expression template class which should not be instantiated to avoid dangling references. But I'm temped to declare a variable with auto and 'auto' create a named instance of a temporary class. How can I disable auto declaration…
4
votes
1 answer

Why deleted copy constructor doesn't let to use other constructor with polymorphic type?

I wonder why this program doesn't compile (the same behavior on msvc, gcc and clang): #include using namespace std; struct Action { virtual void action() { cout << "Action::action()\n"; } }; struct ActionDecorator :…
Piotrek
  • 451
  • 1
  • 5
  • 9
4
votes
1 answer

Why am I using the deleted function 'void std::ref(const _Tp&&) [with _Tp = int]'

#include #include #include #include using namespace std; int main() { int i = 0; auto p = make_pair(ref(i), ref(i++)); p.first++; p.second++; cout << "i = " << i << endl; } For example if I…
Yyh
  • 59
  • 1
  • 8
4
votes
2 answers

Why can't an object containing a ostringstream member be constructed?

I have the following class example, simplified from a larger project. It's based on a logging framework that uses the logger's scope to terminate a log entry in the destructor. The code below will not compile because the constructor is an implicitly…
Ethan T
  • 1,390
  • 12
  • 28
4
votes
1 answer

Reference qualifiers and deleted member methods

Consider the following code: #include struct S { void f(int) = delete; void f(int) && { } }; int main() { } It doesn't compile saying that the member method cannot be overloaded and it makes sense, of course. On the other side,…
skypjack
  • 49,335
  • 19
  • 95
  • 187
4
votes
3 answers

Forbids functions with `static_assert`

I want to prevent certain functions from being called. Let's ignore the case of calling the function via a function pointer or something, and just concentrate on the case of direct function call. I can do this with = delete. However, the diagnostic…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
3
votes
1 answer

Effect of user deleted auto constructors on implicit generation of copy constructors

How does user deleted auto constructors affect implicit generation of copy constructors? For eg: struct Foo { Foo(X) = delete; auto operator=(X) = delete; }; int main() { Foo a; Foo b(a); b = a; } In the above struct, if X is…
3
votes
1 answer

Is "implicitly deleted" constructor =delete or not declared at all?

Given the following toy code: class X { public: X() { } X(const X&) { } //X(X&&) = delete; }; int main() { X x; X y = std::move(x); } I know that X::X(X&&) is implicitly deleted in this case because X(const X&) exists as an…
CPPL
  • 726
  • 1
  • 10
3
votes
0 answers

Why my compiler doesn't allow overriding a deleted non-throwing virtual member function as a deleted throwing member function?

I've read from C++ primer 5th ed. That a virtual member function that won't throw (noexcept) must be overriden as non-throwing function. The exception is if the virtual member function is defined as a 'deleted' member. So I've tried this: struct…
Maestro
  • 2,512
  • 9
  • 24
3
votes
4 answers

Opening stream via function

I need help with the non-copyable nature of [io](f)streams. I need to provide a hackish wrapper around fstreams in order to handle files with unicode characters in their filenames on Windows. For this, I devised a wrapper function: bool…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
3
votes
3 answers

C++11 deleted/defaulted constructors

I'm a bit confused about how/why the constructors are called in C++11 and C++17. #include using namespace std; //--- template struct StructTest { public: const T Var = -1; //--- // constexpr StructTest() = delete; …
3
votes
2 answers

C++: Deleted function when trying to pass fstream as argument?

I don't know what is wrong with my code. I am trying to get a file path from the console for two files, then I initialize some fstream objects with those files and ios::in | ios::out for one, and an addition of ios::binary for the other. Here are…
vasilescur
  • 240
  • 2
  • 5
  • 16
3
votes
1 answer

'use of deleted function' when merging two vectors of unique_ptr

I'm trying to merge two vectors of unique_ptr (i.e. std::move them out from one and into another) and I keep running into a "use of deleted function..." wall of error text. According to the error, I am apparently trying to use unique_ptr's deleted…
AUD_FOR_IUV
  • 473
  • 1
  • 3
  • 16
3
votes
1 answer

Compiler error when instantiating std::mutex

I have used std::mutex extensively in my codebase. However, one of the classes simply does not let me add a mutex to its instance variables list. I am instantiating the mutex quite simply using the following - std::mutex myMutex; I added the above…
The Vivandiere
  • 3,059
  • 3
  • 28
  • 50
3
votes
1 answer

using range-based for with std::set> deleted function

I'm trying to use a range based iterator with a set of unique_ptr instances but I'm getting the following compilation error: C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>>…
jhegedus
  • 61
  • 2
  • 8