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

Why - in that example with an extended lambda - is an ambigious copy constructor and some deleted function

I do not understand the behaviour of the following code: template< bool b > struct Foo { Foo() = default; __host__ Foo( const Foo & ) requires( b ) {} __device__ Foo( const Foo & ) requires( !b ) {} }; template< typename Lambda…
tommsch
  • 582
  • 4
  • 19
2
votes
1 answer

std::map: Creating/replacing an element when it is not default-constructible

Suppose you have a class which is not default constructible. class A { private: int a; public: A() = delete; A(int a0) : a(a0) {} }; Now, we have some map Int --> A, std::map mapping. Let's say we want to create a new…
buj
  • 165
  • 5
2
votes
2 answers

C++ Problems Pushing Initializer List onto a Standard Vector If the Struct Contains a Mutex

I'm currently working on a project in C++ in which I have a list of structs stored in a vector that have a lot processing associated with them. In order to speed things up, I've chosen to split the program across multiple threads, and the lazy way…
2
votes
0 answers

Will containing a `std::unique_ptr` make the default copy constructor a deleted function?

The fact that unique_ptr doesn't allow copy which means no more than one object can share the same underlying data. If I contain an object of unique_ptr in my class will the compiler be wise enough to make the implicit copy constructor a deleted…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
2
votes
2 answers

How may I forbid calls to const member function of an rvalue object in C++ 2011?

The following code #include #include #include std::string const& at(std::vector const& n, int i) { return n[i]; } std::vector mkvec() { std::vector n; …
neuront
  • 9,312
  • 5
  • 42
  • 71
2
votes
2 answers

Inheritance of =delete functions

let's say I have a class named File. I want to disable the copy constructor for every son of File, for example TextFile. Would doing something like this will still disable the copy constructor of TextFile? class File { public: File(const File&…
איתן לוי
  • 433
  • 1
  • 3
  • 9
2
votes
1 answer

Copying parameter invokes deleted constructor when that constructor shouldn't be called

#include template class Wrapper { public: Wrapper() = delete; Wrapper(const Wrapper&) = delete; Wrapper(Wrapper&&) = delete; ~Wrapper() = default; Wrapper(const T&) = delete; Wrapper(T&& in) :…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
2 answers

c++ base class invokes a deleted or inaccessible function

I have a player variable which contains a vector of Resource class which derived from Name and ID class. Problem lies when I'm compiling the code and the following errors appear during compilation. resources.h(27): note: 'Resources…
CraftedGaming
  • 499
  • 7
  • 21
2
votes
1 answer

Use a C++ template to define a function but explicitly delete it to prevent misuse

I have a template class with a constructor in which I want to explicitly forbid the usage of any type beyond a well defined list, like this: template class MyClass { public: MyClass(int &); MyClass(float &); …
2
votes
2 answers

Deleting conversion operators

Is there a way to disable conversion operators? Marking them "= delete" messes up other things. Consider the following code: class Foo { public: Foo() :mValue(0) {} ~Foo() = default; Foo(int64_t v) { mValue = v; } Foo(const Foo&…
2
votes
1 answer

How to forbid empty list initialization?

I have a class, that doesn't have any members at all. And so, it is not intended to be instantiated. So, I deleted default c-r. That forbids any construction except list-initialization. Is there any way to forbid it too? class Empty{ //No non-static…
2
votes
0 answers

Initialization of class with deleted default constructor in different c++11 standard versions

In the duplicate there is no answer to the points 1 and 4 of my question at least. And they are the most important. I can delete the other points but I ask not to close the whole question. 1. In code below obj1 is created normally. But if I try to…
JenyaKh
  • 2,040
  • 17
  • 25
2
votes
1 answer

Non instatiable class by deleting destructor?

I have a class that I use for purely syntactic purposes, to call a function in a certain way. This is a simplified example: #include template struct make{ template static T1 from(T2 const& t2){ return…
alfC
  • 14,261
  • 4
  • 67
  • 118
2
votes
2 answers

error C2280: attempting to reference a deleted function (trying to call vector.erase)

I'm new to C++. I'm developing a breakout-clone with SFML and Box2D, and I get this error when compiling. Details of the error: c:\program files (x86)\visual studio express 2013\vc\include\xutility(2420): error C2280: 'Tile &Tile::operator =(const…
lefti
  • 268
  • 4
  • 13
1
vote
1 answer

Find Functions Stored in Running Python Kernel

I've done something stupid in Python, in Jupyter notebook. I deleted the cell that had my functions in it, probably a couple of hours ago, and now I don't have them any more. However, I can still run them, so they are still loaded in the kernel. Is…
Anna
  • 21
  • 3