When an Empty Destructor is Required -- Andreas Fertig
In my previous post, "Why you shouldn't provide an empty destructor," we discussed the importance of not providing an empty destructor in most cases. However, in this post, we'll explore a rare exception to this rule that arises when using the PImpl idiom to hide implementation details, specifically in situations where the unique_ptr encounters issues with incomplete types. Let's delve into this exception and learn when it's necessary to provide an empty destructor.
When an Empty Destructor is Required
by Andreas Fertig
From the article:
As you probably know from life, not only programming. There is no rule without an exception. Let's talk about the one case that requires an empty destructor (yes, I know it sounds silly). Suppose you have the following code:
In file included from <source>:2: In file included from /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/memory:76: /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/unique_ptr.h:93:16: error: invalid application of 'sizeof' to an incomplete type 'Orange' static_assert(sizeof(_Tp)>0, ^~~~~~~~~~~ /opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/unique_ptr.h:396:4: note: in instantiation of member function 'std::default_delete<Orange>::operator()' requested here get_deleter()(std::move(__ptr)); ^ <source>:4:7: note: in instantiation of member function 'std::unique_ptr<Orange>::~unique_ptr' requested here class Apple ^ <source>:6:27: note: forward declaration of 'Orange' std::unique_ptr<class Orange> mOrange{}; ^ 1 error generated. Compiler returned: 1
Essentially, this message tells you that thestd::unique_ptrcannot instantiate the required destructor for itself typed withOrange. The simple reason is that, at this point,Orangehas no visible destructor. Which is the entire point of the PImpl idiom. What now?



Release 1.84.0 of the Boost C++ Libraries is now available.
A new episode of the series about SObjectizer and message passing:
In this post, we’ll have fun using C++20’s spans to process data on multiple threads. What’s more, we’ll be equipped with the latest concurrency features from C++20.
We’re announcing a free early preview of CLion, which uses the ReSharper C++/Rider C++ language engine instead of the CLion "legacy" engine. The Preview build is available via our dedicated Toolbox App feed. At some point in 2024, depending on the results of the feedback collected, CLion Nova will be merged into CLion. Until then, the Preview build will be free to use and can be installed in parallel with your usual CLion (Classic) installation.
A new episode of the series about SObjectizer and message passing:
A new episode of the series about SObjectizer and message passing:
We’ll look at the basics of data flow analysis, including how it works in general, while presenting several real-world examples where it can help you write better code.
A new episode of the series about SObjectizer and message passing: