{"id":1905,"date":"2020-03-04T09:23:54","date_gmt":"2020-03-04T03:53:54","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1905"},"modified":"2020-03-04T09:31:34","modified_gmt":"2020-03-04T04:01:34","slug":"deleted-destructor-in-c","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/","title":{"rendered":"Deleted Destructor in C++"},"content":{"rendered":"<p>Since <em><strong>C++ 11<\/strong><\/em>, we can use the keyword <em><strong>delete<\/strong><\/em> to declare functions as deleted and thus prohibit the use of these functions in the code. See this <a href=\"https:\/\/www.ibm.com\/developerworks\/community\/blogs\/5894415f-be62-4bc0-81c5-3956e82276f3\/entry\/deleted_functions_in_c_11?lang=en\" target=\"_blank\" rel=\"noopener\"><em><strong>article<\/strong><\/em><\/a>\u00a0for a brief overview.<\/p>\n<p>In particular, the <em><strong>destructor<\/strong><\/em> of a class\/struct\/union can be declared as <em><strong>deleted<\/strong><\/em>. In today\u2019s article, I am going to discuss this specific feature in detail with several examples.<\/p>\n<p>It is useful to recall the basic principle of object creation in C++: When an object of a user-defined type is created, an appropriate <em><strong>constructor<\/strong><\/em> is automatically called, and when the object goes out of life, its <em><strong>destructor<\/strong><\/em> is automatically called (see <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/destructor\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a>\u00a0for the complete destructor semantics). <em><strong>Automatically<\/strong><\/em>, here, means that the compiler generates code behind the scenes to make sure these events happen <em><strong>without explicit intervention<\/strong><\/em> by the programmer.<\/p>\n<p>The following code fragment illustrates this idea:<\/p>\n<figure id=\"attachment_1906\" aria-describedby=\"caption-attachment-1906\" style=\"width: 462px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1906\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg\" data-orig-size=\"462,399\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;}\" data-image-title=\"Example-1\" data-image-description=\"&lt;p&gt;Example-1&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-1&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg\" class=\"size-full wp-image-1906\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg?resize=462%2C399&#038;ssl=1\" alt=\"Example-1\" width=\"462\" height=\"399\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg?w=462&amp;ssl=1 462w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-1.jpg?resize=300%2C259&amp;ssl=1 300w\" sizes=\"(max-width: 462px) 100vw, 462px\" \/><\/a><figcaption id=\"caption-attachment-1906\" class=\"wp-caption-text\"><strong>Example-1<\/strong><\/figcaption><\/figure>\n<p>If the programmer does not define a destructor explicitly, then the destructor is implicitly defined by the compiler. This can be suppressed by using <em><strong>= delete<\/strong><\/em> in the destructor declaration:<\/p>\n<figure id=\"attachment_1920\" aria-describedby=\"caption-attachment-1920\" style=\"width: 227px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Deleted-dtor.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1920\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/deleted-dtor\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Deleted-dtor.jpg\" data-orig-size=\"227,85\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583313223&quot;}\" data-image-title=\"Deleted Destructor\" data-image-description=\"&lt;p&gt;Deleted Destructor&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Deleted Destructor&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Deleted-dtor.jpg\" class=\"size-full wp-image-1920\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Deleted-dtor.jpg?resize=227%2C85&#038;ssl=1\" alt=\"Deleted Destructor\" width=\"227\" height=\"85\" \/><\/a><figcaption id=\"caption-attachment-1920\" class=\"wp-caption-text\"><strong>Deleted Destructor<\/strong><\/figcaption><\/figure>\n<p>What effect does this have on the code? Look at the following example.<\/p>\n<figure id=\"attachment_1907\" aria-describedby=\"caption-attachment-1907\" style=\"width: 417px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1907\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg\" data-orig-size=\"417,444\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583227037&quot;}\" data-image-title=\"Example-2\" data-image-description=\"&lt;p&gt;Example-2&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-2&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg\" class=\"size-full wp-image-1907\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg?resize=417%2C444&#038;ssl=1\" alt=\"Example-2\" width=\"417\" height=\"444\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg?w=417&amp;ssl=1 417w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-2.jpg?resize=282%2C300&amp;ssl=1 282w\" sizes=\"(max-width: 417px) 100vw, 417px\" \/><\/a><figcaption id=\"caption-attachment-1907\" class=\"wp-caption-text\"><strong>Example-2<\/strong><\/figcaption><\/figure>\n<p>Statements numbered (1), (3) and (5) are OK only if a valid destructor is present, and since the destructor is <em><strong>deleted<\/strong><\/em> in this case, the compiler generates error messages as expected.<\/p>\n<p>What the above example also shows is that we can only create objects of class <em><strong>A<\/strong><\/em> on the <em><strong>heap<\/strong><\/em>, but then we cannot <em><strong>delete<\/strong><\/em> such an object. That is, of course, not a good practice because it will lead to memory leaks. What can we do in such situations?<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>What if we use the smart pointer <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/unique_ptr\" target=\"_blank\" rel=\"noopener\"><em><strong>unique_ptr<\/strong><\/em><\/a>\u00a0to manage our object?<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_1908\" aria-describedby=\"caption-attachment-1908\" style=\"width: 398px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1908\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg\" data-orig-size=\"398,362\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583241117&quot;}\" data-image-title=\"Example-3\" data-image-description=\"&lt;p&gt;Example-3&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-3&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg\" class=\"size-full wp-image-1908\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg?resize=398%2C362&#038;ssl=1\" alt=\"Example-3\" width=\"398\" height=\"362\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg?w=398&amp;ssl=1 398w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-3.jpg?resize=300%2C273&amp;ssl=1 300w\" sizes=\"(max-width: 398px) 100vw, 398px\" \/><\/a><figcaption id=\"caption-attachment-1908\" class=\"wp-caption-text\"><strong>Example-3<\/strong><\/figcaption><\/figure>\n<p>Unfortunately, this does not solve our problem, because when the <em><strong>unique_ptr<\/strong><\/em> object goes out of scope, it (by default) applies <em><strong>delete<\/strong><\/em> on the managed object, leading to a call of the destructor, and thus triggering the compiler error!<\/p>\n<p>Fortunately, there is a way to address this problem. <em><strong>unique_ptr<\/strong><\/em> allows us to supply a custom <em><strong>deleter<\/strong><\/em>, which will be called when the object goes out of scope. Therefore, we define a (static) method in our class and pass a pointer to this function to the <em><strong>unique_ptr<\/strong><\/em> instance, along with our pointer.<\/p>\n<figure id=\"attachment_1909\" aria-describedby=\"caption-attachment-1909\" style=\"width: 503px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1909\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-4-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg\" data-orig-size=\"503,419\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583241253&quot;}\" data-image-title=\"Example-4\" data-image-description=\"&lt;p&gt;Example-4&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-4&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg\" class=\"size-full wp-image-1909\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg?resize=503%2C419&#038;ssl=1\" alt=\"Example-4\" width=\"503\" height=\"419\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg?w=503&amp;ssl=1 503w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-4.jpg?resize=300%2C250&amp;ssl=1 300w\" sizes=\"(max-width: 503px) 100vw, 503px\" \/><\/a><figcaption id=\"caption-attachment-1909\" class=\"wp-caption-text\"><strong>Example-4<\/strong><\/figcaption><\/figure>\n<p>This time we do not get any error because our destructor is not called. But it is very important to note that the responsibility for releasing the memory associated with our <em><strong>A<\/strong><\/em> object (obtained through <em><strong>new<\/strong><\/em> operator) rests with <em><strong>A::Deleter()<\/strong><\/em> function. Failure to handle this correctly will lead to runtime problems.<\/p>\n<h3>Effect on Inheritance<\/h3>\n<p>So far, we have considered different cases where the class with <em><strong>deleted destructor<\/strong><\/em> is used directly. What would happen if another class, say <em><strong>B<\/strong><\/em>, is derived from <em><strong>A<\/strong><\/em>?<\/p>\n<p>When a class is designed to act as a base class in a hierarchy, its destructor is usually <em><strong>virtual<\/strong><\/em> (see this <a href=\"http:\/\/www.gotw.ca\/publications\/mill18.htm\" target=\"_blank\" rel=\"noopener\"><em><strong>article<\/strong><\/em><\/a>). In accordance with this guideline, let us declare <em><strong>A::~A()<\/strong><\/em> as <em><strong>virtual<\/strong><\/em> and <em><strong>deleted<\/strong><\/em>. See what happens in this case.<\/p>\n<figure id=\"attachment_1910\" aria-describedby=\"caption-attachment-1910\" style=\"width: 265px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1910\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5.jpg\" data-orig-size=\"265,275\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583244383&quot;}\" data-image-title=\"Example-5\" data-image-description=\"&lt;p&gt;Example-5&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-5&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5.jpg\" class=\"size-full wp-image-1910\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5.jpg?resize=265%2C275&#038;ssl=1\" alt=\"Example-5\" width=\"265\" height=\"275\" \/><\/a><figcaption id=\"caption-attachment-1910\" class=\"wp-caption-text\"><strong>Example-5<\/strong><\/figcaption><\/figure>\n<p>The program does not compile. My <em><strong>Visual Studio 2019 (ver 16.4.5)<\/strong><\/em> gives the following errors:<\/p>\n<figure id=\"attachment_1911\" aria-describedby=\"caption-attachment-1911\" style=\"width: 496px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1911\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-5-errors\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg\" data-orig-size=\"496,84\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583244413&quot;}\" data-image-title=\"Example-5 Compiler Errors\" data-image-description=\"&lt;p&gt;Example-5 Compiler Errors&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-5 Compiler Errors&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg\" class=\"size-full wp-image-1911\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg?resize=496%2C84&#038;ssl=1\" alt=\"Example-5 Compiler Errors\" width=\"496\" height=\"84\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg?w=496&amp;ssl=1 496w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-5-Errors.jpg?resize=300%2C51&amp;ssl=1 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/a><figcaption id=\"caption-attachment-1911\" class=\"wp-caption-text\"><strong>Example-5 Compiler Errors<\/strong><\/figcaption><\/figure>\n<p>Could it be because <em><strong>A::~A()<\/strong><\/em> is <em><strong>virtual<\/strong><\/em>? Let us see what happens if we remove the <em><strong>virtual<\/strong><\/em> keyword:<\/p>\n<figure id=\"attachment_1912\" aria-describedby=\"caption-attachment-1912\" style=\"width: 220px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1912\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6.jpg\" data-orig-size=\"220,274\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583244661&quot;}\" data-image-title=\"Example-6\" data-image-description=\"&lt;p&gt;Example-6&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-6&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6.jpg\" class=\"size-full wp-image-1912\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6.jpg?resize=220%2C274&#038;ssl=1\" alt=\"Example-6\" width=\"220\" height=\"274\" \/><\/a><figcaption id=\"caption-attachment-1912\" class=\"wp-caption-text\"><strong>Example-6<\/strong><\/figcaption><\/figure>\n<p>Unfortunately, even this does not go through. Here are the errors:<\/p>\n<figure id=\"attachment_1913\" aria-describedby=\"caption-attachment-1913\" style=\"width: 508px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1913\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-6-errors\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg\" data-orig-size=\"508,63\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583244813&quot;}\" data-image-title=\"Example-6 Compiler Errors\" data-image-description=\"&lt;p&gt;Example-6 Compiler Errors&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-6 Compiler Errors&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg\" class=\"size-full wp-image-1913\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg?resize=508%2C63&#038;ssl=1\" alt=\"Example-6 Compiler Errors\" width=\"508\" height=\"63\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg?w=508&amp;ssl=1 508w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-6-Errors.jpg?resize=300%2C37&amp;ssl=1 300w\" sizes=\"(max-width: 508px) 100vw, 508px\" \/><\/a><figcaption id=\"caption-attachment-1913\" class=\"wp-caption-text\"><strong>Example-6 Compiler Errors<\/strong><\/figcaption><\/figure>\n<p>From the above two examples (and variants of these examples), we can conclude that it is <em><strong>impossible to meaningfully derive any class from a class with deleted destructor<\/strong><\/em>.<\/p>\n<p>Let us consider the other possibility where a class with deleted destructor is derived from a normal base class.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_1914\" aria-describedby=\"caption-attachment-1914\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1914\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7.jpg\" data-orig-size=\"300,275\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583309695&quot;}\" data-image-title=\"Example-7\" data-image-description=\"&lt;p&gt;Example-7&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-7&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7.jpg\" class=\"size-full wp-image-1914\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7.jpg?resize=300%2C275&#038;ssl=1\" alt=\"Example-7\" width=\"300\" height=\"275\" \/><\/a><figcaption id=\"caption-attachment-1914\" class=\"wp-caption-text\"><strong>Example-7<\/strong><\/figcaption><\/figure>\n<p>We get an error when we compile this code (notice we have not instantiated <em><strong>Y<\/strong> <\/em>at all):<\/p>\n<figure id=\"attachment_1915\" aria-describedby=\"caption-attachment-1915\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1915\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-7-errors\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg\" data-orig-size=\"400,46\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583258656&quot;}\" data-image-title=\"Example-7 Compiler Errors\" data-image-description=\"&lt;p&gt;Example-7 Compiler Errors&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-7 Compiler Errors&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg\" class=\"size-full wp-image-1915\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg?resize=400%2C46&#038;ssl=1\" alt=\"Example-7 Compiler Errors\" width=\"400\" height=\"46\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg?w=400&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-7-Errors.jpg?resize=300%2C35&amp;ssl=1 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-1915\" class=\"wp-caption-text\"><strong>Example-7 Compiler Errors<\/strong><\/figcaption><\/figure>\n<p>Since the base destructor is virtual, destructors of all derived classes are virtual, and according to the language, it is not OK to <em><strong>delete<\/strong><\/em> an <em><strong>overriding<\/strong><\/em> virtual destructor. Hence the error. What if the base class destructor is not virtual?<\/p>\n<figure id=\"attachment_1916\" aria-describedby=\"caption-attachment-1916\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1916\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg\" data-orig-size=\"522,465\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583309652&quot;}\" data-image-title=\"Example-8\" data-image-description=\"&lt;p&gt;Example-8&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-8&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg\" class=\"size-full wp-image-1916\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?resize=522%2C465&#038;ssl=1\" alt=\"Example-8\" width=\"522\" height=\"465\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?w=522&amp;ssl=1 522w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?resize=300%2C267&amp;ssl=1 300w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/><\/a><figcaption id=\"caption-attachment-1916\" class=\"wp-caption-text\"><strong>Example-8<\/strong><\/figcaption><\/figure>\n<p>The above is similar to <em><strong>Example-2<\/strong><\/em>, except for lines 18 and 19. While it is fine for a base class pointer to point to a derived class, it is not correct to delete the derived object through the base pointer when the base destructor is not virtual. This can cause undefined behaviour.<\/p>\n<h3>Effect on Composition<\/h3>\n<p>The next case we will consider is one where an object of a class with deleted destructor, is a <em><strong>member of another class<\/strong><\/em>. Look at the following code.<\/p>\n<figure id=\"attachment_1917\" aria-describedby=\"caption-attachment-1917\" style=\"width: 277px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1917\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg\" data-orig-size=\"277,381\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583310271&quot;}\" data-image-title=\"Example-9\" data-image-description=\"&lt;p&gt;Example-9&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-9&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg\" class=\"size-full wp-image-1917\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg?resize=277%2C381&#038;ssl=1\" alt=\"Example-9\" width=\"277\" height=\"381\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg?w=277&amp;ssl=1 277w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9.jpg?resize=218%2C300&amp;ssl=1 218w\" sizes=\"(max-width: 277px) 100vw, 277px\" \/><\/a><figcaption id=\"caption-attachment-1917\" class=\"wp-caption-text\"><strong>Example-9<\/strong><\/figcaption><\/figure>\n<p>In the above example, the struct <em><strong>Compose<\/strong><\/em> contains a data member of type <em><strong>A.<\/strong><\/em> As expected, we get errors when we compile this code:<\/p>\n<figure id=\"attachment_1918\" aria-describedby=\"caption-attachment-1918\" style=\"width: 515px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1918\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/example-9-errors\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg\" data-orig-size=\"515,113\" data-comments-opened=\"1\" data-image-meta=\"{&quot;credit&quot;:&quot;Admin&quot;,&quot;created_timestamp&quot;:&quot;1583310292&quot;}\" data-image-title=\"Example-9 Compiler Errors\" data-image-description=\"&lt;p&gt;Example-9 Compiler Errors&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-9 Compiler Errors&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg\" class=\"size-full wp-image-1918\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg?resize=515%2C113&#038;ssl=1\" alt=\"Example-9 Compiler Errors\" width=\"515\" height=\"113\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg?w=515&amp;ssl=1 515w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-9-Errors.jpg?resize=300%2C66&amp;ssl=1 300w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/a><figcaption id=\"caption-attachment-1918\" class=\"wp-caption-text\"><strong>Example-9 Compiler Errors<\/strong><\/figcaption><\/figure>\n<p>So, what do you think of <em><strong>deleted destructor<\/strong><\/em>? Should we use it? My general view is that if the situation warrants it, is up to the programmer to use any feature the language supports. After all, if deleting a destructor is never correct, then the language could have forbidden it. Having said that, I wish to add that a deleted destructor is probably quite rare in real world projects. I personally consider a <em><strong>private (or protected) destructor<\/strong><\/em> as a meaningful alternative to <em><strong>deleted destructor<\/strong><\/em>. <span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Hope you found today\u2019s discussion informative. You can download the example programs from <a href=\"http:\/\/www.rangakrish.com\/downloads\/Examples.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since C++ 11, we can use the keyword delete to declare functions as deleted and thus prohibit the use of these functions in the code. See this article\u00a0for a brief overview. In particular, the destructor of a class\/struct\/union can be declared as deleted. In today\u2019s article, I am going to discuss this specific feature in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49],"tags":[67,230],"class_list":["post-1905","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-destructor"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-uJ","jetpack-related-posts":[{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":1905,"position":0},"title":"std::is_destructible","author":"admin","date":"March 15, 2020","format":false,"excerpt":"In the last article, I explained the deleted destructor in some detail. Today, I would like to talk about a related construct, a type trait called std::is_destructible. Type traits, defined in the header <type_traits>, are a big help when it comes to implementing template metaprogramming. \u00a0See this\u00a0article for a nice\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Private\/Protected Destructor","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2011,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/","url_meta":{"origin":1905,"position":1},"title":"std::is_trivial","author":"admin","date":"June 6, 2020","format":false,"excerpt":"Today let us try to understand the type trait std::is_trivial<T>. This trait checks if the given type is a trivial type. For a precise definition of what trivial means, please visit the official page. As usual, we will go through three cases: - Primitive types - Classes without inheritance -\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/output2.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":1905,"position":2},"title":"std::is_standard_layout","author":"admin","date":"June 21, 2020","format":false,"excerpt":"In our last post, we learnt about the type trait std::is_trivial<T>. Today, let us go through another type trait that is quite similar. The expression\u00a0is_standard_layout<T>::value returns true if the layout of objects of type T is compiler independent, and hence is of standard format. Else, it returns false. This is\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"The Difference","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":3691,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/05\/04\/beyond-ownership-understanding-the-role-of-stdweak_ptr-in-modern-c\/","url_meta":{"origin":1905,"position":3},"title":"Beyond Ownership: Understanding the Role of std::weak_ptr in Modern C++","author":"admin","date":"May 4, 2025","format":false,"excerpt":"Smart Pointers in C++ are elegant abstractions for managing dynamic memory safely, avoiding dangling pointers and preventing leaks. While std::unique_ptr and std::shared_ptr are well understood and widely used, std::weak_ptr often demands a deeper dive to use correctly. In this article, I will attempt to explain what it is and where\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Circular Reference Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/05\/code1-300x289.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":1905,"position":4},"title":"C++17: std::optional","author":"admin","date":"November 21, 2017","format":false,"excerpt":"Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This failure can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, -1). Throwing an exception is a strong form of failure and\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1091,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","url_meta":{"origin":1905,"position":5},"title":"C++17 &#8211; std::apply() and std::invoke()","author":"admin","date":"October 14, 2018","format":false,"excerpt":"Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case. Lisp, for example, has apply and funcall. When using apply, you can see that the arguments are passed via a separate list object. With funcall,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Calling Member Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=1905"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1905\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}