{"id":1928,"date":"2020-03-15T11:08:41","date_gmt":"2020-03-15T05:38:41","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1928"},"modified":"2020-03-15T11:11:34","modified_gmt":"2020-03-15T05:41:34","slug":"stdis_destructible","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","title":{"rendered":"std::is_destructible"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/\" target=\"_blank\" rel=\"noopener\"><em><strong>last article<\/strong><\/em><\/a>, I explained the <em><strong>deleted destructor<\/strong><\/em> in some detail. Today, I would like to talk about a related construct, a <em><strong>type trait<\/strong><\/em> called <em><strong>std::is_destructible<\/strong><\/em>.<\/p>\n<p>Type traits, defined in the header <em><strong>&lt;type_traits&gt;<\/strong><\/em>, are a big help when it comes to implementing <em><strong>template metaprogramming<\/strong><\/em>. <span class=\"Apple-converted-space\">\u00a0<\/span>See <a href=\"https:\/\/blog.galowicz.de\/2016\/02\/18\/what_is_a_type_trait\/\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a>\u00a0article for a nice introduction to type traits in C++.<\/p>\n<p>The <a href=\"http:\/\/www.cplusplus.com\/reference\/type_traits\/is_destructible\/\" target=\"_blank\" rel=\"noopener\"><em><strong>std::is_destructible<\/strong><\/em><\/a> trait checks whether objects of the given type <em><strong>T<\/strong><\/em> can be <em><strong>destroyed<\/strong><\/em>. What does this mean? Let us go through some examples.<\/p>\n<figure id=\"attachment_1929\" aria-describedby=\"caption-attachment-1929\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1929\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example1a\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg\" data-orig-size=\"660,590\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584174735&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Some structs\" data-image-description=\"&lt;p&gt;Some structs&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Some structs&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg\" class=\"wp-image-1929\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg?resize=650%2C581&#038;ssl=1\" alt=\"Some structs\" width=\"650\" height=\"581\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg?w=660&amp;ssl=1 660w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1a.jpg?resize=300%2C268&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1929\" class=\"wp-caption-text\"><strong>Some structs<\/strong><\/figcaption><\/figure>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span>The above defines several <em><strong>structs<\/strong><\/em>. Here is the <em><strong>main<\/strong><\/em>\u00a0function that applies <em><strong>std::is_destructible&lt;T&gt;<\/strong><\/em> on the above structs.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_1930\" aria-describedby=\"caption-attachment-1930\" style=\"width: 649px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1930\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example1b-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg\" data-orig-size=\"658,297\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584174765&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Applying std::is_destructible\" data-image-description=\"&lt;p&gt;Applying std::is_destructible&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Applying std::is_destructible&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg\" class=\"wp-image-1930\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg?resize=649%2C293&#038;ssl=1\" alt=\"Applying std::is_destructible\" width=\"649\" height=\"293\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg?w=658&amp;ssl=1 658w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1b.jpg?resize=300%2C135&amp;ssl=1 300w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><figcaption id=\"caption-attachment-1930\" class=\"wp-caption-text\"><strong>Applying std::is_destructible<\/strong><\/figcaption><\/figure>\n<p>All these types have either the default or an explicit destructor, and hence objects of these types can be destroyed in the normal way. So, as expected, the program prints <em><strong>true<\/strong><\/em> for all of them.<\/p>\n<p>Note that we do not have to create objects of these types in order to check if<span class=\"Apple-converted-space\">\u00a0 <\/span>they are destructible.<\/p>\n<p>Some of you may be wondering if <em><strong>std::is_destructible<\/strong><\/em> depends on <em><strong>compile time<\/strong><\/em> or <em><strong>run time<\/strong><\/em> check. The answer is that this is purely a <strong><em>compile time<\/em><\/strong> check! Isn&#8217;t that interesting? Look at the modified <em><strong>main<\/strong><\/em> below:<\/p>\n<figure id=\"attachment_1931\" aria-describedby=\"caption-attachment-1931\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1931\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example2-14\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg\" data-orig-size=\"669,256\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584175733&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Compile Time Check\" data-image-description=\"&lt;p&gt;Compile Time Check&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compile Time Check&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg\" class=\"wp-image-1931\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg?resize=650%2C249&#038;ssl=1\" alt=\"Compile Time Check\" width=\"650\" height=\"249\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg?w=669&amp;ssl=1 669w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2.jpg?resize=300%2C115&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1931\" class=\"wp-caption-text\"><strong>Compile Time Check<\/strong><\/figcaption><\/figure>\n<p>The above code compiles without any error, but does not print anything at run time. You can read about <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/static_assert\" target=\"_blank\" rel=\"noopener\"><em><strong>static_assert<\/strong><\/em><\/a> here.<\/p>\n<p>OK. Let us now consider some examples where the <em><strong>destructor<\/strong><\/em> has been <em><strong>deleted<\/strong><\/em>.<\/p>\n<figure id=\"attachment_1932\" aria-describedby=\"caption-attachment-1932\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1932\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example3-13\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3.jpg\" data-orig-size=\"678,738\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584177276&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&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\/Example3.jpg\" class=\"wp-image-1932\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3.jpg?resize=650%2C708&#038;ssl=1\" alt=\"Deleted Destructor\" width=\"650\" height=\"708\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3.jpg?w=678&amp;ssl=1 678w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3.jpg?resize=276%2C300&amp;ssl=1 276w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1932\" class=\"wp-caption-text\"><strong>Deleted Destructor<\/strong><\/figcaption><\/figure>\n<p>In all the above cases, objects of these types cannot be destroyed.\u00a0If you recall our discussion on deleted destructors in the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/\" target=\"_blank\" rel=\"noopener\"><em><strong>earlier article<\/strong><\/em><\/a>, you will agree that the above behaviour is as expected.<\/p>\n<p>Can we apply <em><strong>std::is_destructible<\/strong><\/em> to primitive types? Of course. See below:<\/p>\n<figure id=\"attachment_1934\" aria-describedby=\"caption-attachment-1934\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1934\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example4-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg\" data-orig-size=\"736,508\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584193723&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Primitive Types\" data-image-description=\"&lt;p&gt;Primitive Types&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Primitive Types&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg\" class=\"wp-image-1934\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg?resize=650%2C449&#038;ssl=1\" alt=\"Primitive Types\" width=\"650\" height=\"449\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg?w=736&amp;ssl=1 736w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4.jpg?resize=300%2C207&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1934\" class=\"wp-caption-text\"><strong>Primitive Types<\/strong><\/figcaption><\/figure>\n<p>Notice that the output is <em><strong>true<\/strong><\/em> in all except the last two cases.<\/p>\n<p>Although it is not directly relevant, let us also consider the case of a <em><strong>destructor<\/strong><\/em> being <em><strong>private<\/strong><\/em> or <em><strong>protected<\/strong><\/em>. The following code fragment shows what happens in these cases.<\/p>\n<figure id=\"attachment_1940\" aria-describedby=\"caption-attachment-1940\" style=\"width: 584px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1940\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example7-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg\" data-orig-size=\"584,819\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584270112&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Private\/Protected Destructor\" data-image-description=\"&lt;p&gt;Private\/Protected Destructor&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Private\/Protected Destructor&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg\" class=\"size-full wp-image-1940\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=584%2C819&#038;ssl=1\" alt=\"Private\/Protected Destructor\" width=\"584\" height=\"819\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?w=584&amp;ssl=1 584w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=214%2C300&amp;ssl=1 214w\" sizes=\"(max-width: 584px) 100vw, 584px\" \/><\/a><figcaption id=\"caption-attachment-1940\" class=\"wp-caption-text\"><strong>Private\/Protected Destructor<\/strong><\/figcaption><\/figure>\n<p>Although <em><strong>X<\/strong><\/em> destructor is accessible from within <em><strong>X::foo()<\/strong><\/em>, <em><strong>is_destructible&lt;X&gt;<\/strong><\/em> returns <em><strong>false<\/strong><\/em>!<\/p>\n<h2>std::is_trivially_destructible<\/h2>\n<p>This is a variant of <em><strong>std::is_destructible<\/strong><\/em>. See <a href=\"http:\/\/www.cplusplus.com\/reference\/type_traits\/is_trivially_destructible\/\" target=\"_blank\" rel=\"noopener\"><em><strong>this page<\/strong><\/em><\/a> for the official specification. Here are some examples:<\/p>\n<figure id=\"attachment_1935\" aria-describedby=\"caption-attachment-1935\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1935\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example5-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg\" data-orig-size=\"788,820\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584194538&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"std::is_trivially_destructible\" data-image-description=\"&lt;p&gt;std::is_trivially_destructible&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;std::is_trivially_destructible&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg\" class=\"wp-image-1935\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg?resize=650%2C676&#038;ssl=1\" alt=\"std::is_trivially_destructible\" width=\"650\" height=\"676\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg?w=788&amp;ssl=1 788w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg?resize=288%2C300&amp;ssl=1 288w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example5.jpg?resize=768%2C799&amp;ssl=1 768w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1935\" class=\"wp-caption-text\"><strong>std::is_trivially_destructible<\/strong><\/figcaption><\/figure>\n<p>Hope you understand why <em><strong>line 27<\/strong><\/em> generates <em><strong>false<\/strong><\/em>. The class <em><strong>Z<\/strong><\/em> has a deleted destructor and so it cannot be destroyed. So, if <em><strong>std::is_destructible<\/strong><\/em> is false, then so is <em><strong>std::is_trivially_destructible<\/strong><\/em>.<\/p>\n<h2>std::is_nothrow_destructible<\/h2>\n<p>This is yet another variant. It checks if the underlying <em><strong>T<\/strong><\/em> is destructible, and in addition, if the destructor is present, whether it is declared <em><strong>noexcept(true)<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Here are some examples:<\/p>\n<figure id=\"attachment_1936\" aria-describedby=\"caption-attachment-1936\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1936\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/example6-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg\" data-orig-size=\"686,821\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1584195358&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"std::is_nothrow_destructible\" data-image-description=\"&lt;p&gt;std::is_nothrow_destructible&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;std::is_nothrow_destructible&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg\" class=\"wp-image-1936\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg?resize=650%2C778&#038;ssl=1\" alt=\"std::is_nothrow_destructible\" width=\"650\" height=\"778\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg?w=686&amp;ssl=1 686w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example6.jpg?resize=251%2C300&amp;ssl=1 251w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1936\" class=\"wp-caption-text\"><strong>std::is_nothrow_destructible<\/strong><\/figcaption><\/figure>\n<p>By default, a destructor is <em><strong>noexcept<\/strong><\/em>.<\/p>\n<p>Well, that is it for today. Hope I managed to convey the essence of the traits <em><strong>std::is_destructible<\/strong><\/em>, <em><strong>std::is_trivially_destructible<\/strong><\/em> and <em><strong>std::is_nothrow_destructible<\/strong><\/em>.<\/p>\n<p>You can download the source files from <a href=\"http:\/\/www.rangakrish.com\/downloads\/IsDestructible.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>. I used <em><strong>Visual Studio 2019 ver 16.4.5<\/strong><\/em> to test the examples.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &lt;type_traits&gt;, are a big help when it comes to implementing template metaprogramming. \u00a0See this\u00a0article for a nice introduction to type traits 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_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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}},"categories":[49,17],"tags":[67,231,233,232],"class_list":["post-1928","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-stdis_destructible","tag-stdis_nothrow_destructible","tag-stdis_trivially_destructible"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-v6","jetpack-related-posts":[{"id":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":1928,"position":0},"title":"std::is_base_of","author":"admin","date":"April 26, 2020","format":false,"excerpt":"In our on-going series on C++ Type Traits, today's topic is about the trait std::is_base_of<>. For the official description, see this. std::is_base_of<A, B>::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::<A, A>::value is true. Let\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":1928,"position":1},"title":"std::is_empty","author":"admin","date":"March 28, 2020","format":false,"excerpt":"In the previous post, we looked at the std::is_destructible<T> type trait. Today, let us try to understand another type trait std::is_empty<T>. As per the specification, is_empty<T>::value will return true in the following cases: - The class\/struct has no non-static data member - The class\/struct does not define a virtual function\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example4: Union and Bit Field","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":1928,"position":2},"title":"std::is_scoped_enum","author":"admin","date":"August 13, 2023","format":false,"excerpt":"The type trait \"std::is_scoped_enum<T>::value\" was introduced in C++23 to check whether the type \u201cT\u201d is a scoped enum type. Another way to use this is std::is_scoped_enum_v<T>. Before getting into this trait in detail, let us briefly recap the differences between unscoped and scoped enums. Unscoped Enums Unscoped enums are the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Unscoped Enums","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":1928,"position":3},"title":"C++17: std::variant","author":"admin","date":"November 5, 2017","format":false,"excerpt":"C++17 introduces a new type-safe union in the form of std::variant. At any time, it can store a single value from one of many types. We need to include <variant>\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1\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.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":1928,"position":4},"title":"std::array vs. std::vector","author":"admin","date":"August 9, 2022","format":false,"excerpt":"One of the common questions when starting to study the container abstractions in C++20 is \"Should I use std::array\u00a0or std::vector?\". The correct answer is \"use what is best suited for the current situation\". That is not good enough, obviously. In this short article, I will try to answer this question\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::array Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1960,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","url_meta":{"origin":1928,"position":5},"title":"std::common_type Type Trait","author":"admin","date":"April 12, 2020","format":false,"excerpt":"In today's post, I would like to go over the type trait std::common_type<>. This trait was introduced in C++11. As per the specification, std::common_type<T1, T2, ...Tn>::type refers to a type Tx in the given list, which the rest of the types in the list can be implicitly converted to. This\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\/04\/Fig5.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1928","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=1928"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1928\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}