C ++ 11 Destructor = delete

I saw the constructor = delete the explanation here , but I wonder if I should forbid calls to the destructor. I am trying to use a class like this:

class A { public: static bool foo(const char* filePath); static void foo(const int something); private: A() = delete; ~A(); }; 

Should I also write like ~A() = delete; ? Does it even matter?

+6
source share
3 answers

~A() = delete; is redundant because, since you cannot create an object, there is no point worrying about the destructor.

Actually with your code there is not even a need A() = delete; , because all members of the class are static .
As Lucian rightly mentions in the commentary, it is better to declare such namespace as namespace . Basic data can be made extern / static depending on the requirement.

+9
source

No, that doesn't matter. You can also delete it, but if the constructor is deleted, then deleting the destructor will do nothing more.

The first (remote constructor) reports that it is impossible to create objects. The second (remote destructor) reports that it is impossible to destroy objects.

Please note that you can "create" and initialize objects using various hacks, but in those cases all bets are disabled and the program performs undefined behavior.

+3
source

(Note: in C ++ 20, deleting the aggregate constructor now makes the type impossible to build, so there is no more unexpected behavior)

To add other answers here, you can declare the destructor remote, not closed and not implemented. This is because even with a private remote constructor, you can still use aggregate initialization to instantiate:

demonstration

 class A { public: static bool foo(const char* filePath); static void foo(const int something); private: A() = delete; }; int main(){ A a{}; // whoops I just created an instance } 

However, if you declare the destructor as remote, neither the default construct nor the initialization of the aggregate will work — it will not compile, because the compiler cannot invoke the destructor for A

demonstration

 class A { public: static bool foo(const char* filePath); static void foo(const int something); ~A() = delete; }; int main(){ A a{}; // fails A b; // fails } 

Since you have already declared your destructor closed and not implemented, all other things are redundant. C ++ 11 makes it easy that you don't need private , just = delete for the destructor. Explicitly declaring the destructor as remote is preferred because it tells other programmers that you never intended to implement it. Thus, some programmers might think that you just forgot to turn on the implementation.


You will want to declare both the default constructor and the destructor as deleted in order to protect against dynamic memory allocation (via new or new allocation ), because otherwise we will not get a compilation error, because now we need to call delete or explicitly call the destructor . And if it's convenient for us to forget, then everything is fine

demonstration

 class A { public: static bool foo(const char* filePath); static void foo(const int something); ~A() = delete; }; int main(){ A* a = new A(); // whoops I created an instance // we leak memory } 

Posting a new demo

But removing both the constructor and the destructor prevents this:

demonstration

 class A { public: static bool foo(const char* filePath); static void foo(const int something); ~A() = delete; A() = delete; }; int main(){ A* a = new A(); // doesnt compile A b{}; // doesn't compile A c; // doesn't compile } 

refer to [class.dtor]: “A program is poorly formed if the potentially called destructor is removed or inaccessible from the call context.”

+2
source

Source: https://habr.com/ru/post/1212173/


All Articles