{"id":1946,"date":"2020-03-28T18:26:53","date_gmt":"2020-03-28T12:56:53","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1946"},"modified":"2020-03-28T18:26:53","modified_gmt":"2020-03-28T12:56:53","slug":"stdis_empty","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","title":{"rendered":"std::is_empty<T>"},"content":{"rendered":"<p>In the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/\" target=\"_blank\" rel=\"noopener\"><em><strong>previous post<\/strong><\/em><\/a>, we looked at the <em><strong>std::is_destructible&lt;T&gt;<\/strong><\/em> type trait. Today, let us try to understand another type trait <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_empty\" target=\"_blank\" rel=\"noopener\"><em><strong>std::is_empty&lt;T&gt;.<\/strong><\/em><\/a><\/p>\n<p>As per the specification, <em><strong>is_empty&lt;T&gt;::value<\/strong><\/em> will return <em><strong>true<\/strong><\/em> in the following cases:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">&#8211; The class\/struct has no non-static data member<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">&#8211; The class\/struct does not define a virtual function<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">&#8211; The class\/struct does not derive from a virtual base class<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">&#8211; The class\/struct does not derive from non-empty base classes<\/span><\/p>\n<p>Let us understand this concept by looking at some examples.<\/p>\n<p>Here is our first example:<\/p>\n<figure id=\"attachment_1947\" aria-describedby=\"caption-attachment-1947\" style=\"width: 554px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1947\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example1-18\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg\" data-orig-size=\"554,883\" 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;1585410490&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=\"Example1\" data-image-description=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg\" class=\"size-full wp-image-1947\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg?resize=554%2C883&#038;ssl=1\" alt=\"Example1\" width=\"554\" height=\"883\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg?w=554&amp;ssl=1 554w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1.jpg?resize=188%2C300&amp;ssl=1 188w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/a><figcaption id=\"caption-attachment-1947\" class=\"wp-caption-text\"><strong>Example1<\/strong><\/figcaption><\/figure>\n<p>For convenience, we will use <em><strong>std::is_empty_v&lt;T&gt;<\/strong><\/em> instead of <em><strong>is_empty&lt;T&gt;::value<\/strong><\/em>. Also, just to make it a bit more interesting, I am printing the <em><strong>&#8220;<\/strong><strong>sizeof&#8221;<\/strong><\/em>\u00a0each class.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>When we run this program, we get the following output:<\/p>\n<figure id=\"attachment_1948\" aria-describedby=\"caption-attachment-1948\" style=\"width: 425px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1948\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example1-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg\" data-orig-size=\"425,184\" 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;1585410619&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=\"Example1 Output\" data-image-description=\"&lt;p&gt;Example1 Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example1 Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg\" class=\"size-full wp-image-1948\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg?resize=425%2C184&#038;ssl=1\" alt=\"Example1 Output\" width=\"425\" height=\"184\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg?w=425&amp;ssl=1 425w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example1-Output.jpg?resize=300%2C130&amp;ssl=1 300w\" sizes=\"(max-width: 425px) 100vw, 425px\" \/><\/a><figcaption id=\"caption-attachment-1948\" class=\"wp-caption-text\"><strong>Example1 Output<\/strong><\/figcaption><\/figure>\n<p>The output is along the expected lines. You can see that even if a class is <em><strong>empty<\/strong><\/em>, its size is always <em><strong>greater than zero<\/strong><\/em> (the actual value is implementation dependent).<span class=\"Apple-converted-space\">\u00a0 <\/span>The reason why class <em><strong>E<\/strong><\/em>\u00a0is not <em><strong>empty<\/strong><\/em> is because it has an instance variable (although the class <em><strong>A<\/strong><\/em>\u00a0is empty).<\/p>\n<p>Let us now look at some examples involving inheritance.<\/p>\n<figure id=\"attachment_1950\" aria-describedby=\"caption-attachment-1950\" style=\"width: 569px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1950\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example2-15\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg\" data-orig-size=\"569,845\" 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;1585411851&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=\"Inheritance Examples\" data-image-description=\"&lt;p&gt;Inheritance Examples&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Inheritance Examples&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg\" class=\"size-full wp-image-1950\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg?resize=569%2C845&#038;ssl=1\" alt=\"Inheritance Examples\" width=\"569\" height=\"845\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg?w=569&amp;ssl=1 569w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-1.jpg?resize=202%2C300&amp;ssl=1 202w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/a><figcaption id=\"caption-attachment-1950\" class=\"wp-caption-text\"><strong>Inheritance Examples<\/strong><\/figcaption><\/figure>\n<p>Here is the corresponding output:<\/p>\n<figure id=\"attachment_1951\" aria-describedby=\"caption-attachment-1951\" style=\"width: 436px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1951\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example2-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg\" data-orig-size=\"436,183\" 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;1585411917&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=\"Example2 Output\" data-image-description=\"&lt;p&gt;Example2 Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example2 Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg\" class=\"size-full wp-image-1951\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg?resize=436%2C183&#038;ssl=1\" alt=\"Example2 Output\" width=\"436\" height=\"183\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg?w=436&amp;ssl=1 436w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example2-Output.jpg?resize=300%2C126&amp;ssl=1 300w\" sizes=\"(max-width: 436px) 100vw, 436px\" \/><\/a><figcaption id=\"caption-attachment-1951\" class=\"wp-caption-text\"><strong>Example2 Output<\/strong><\/figcaption><\/figure>\n<p>The behaviors of classes <em><strong>A, B, C<\/strong><\/em>, and <em><strong>D<\/strong><\/em> are as expected. However, I am surprised by the output corresponding to class <em><strong>E<\/strong><\/em>. I think this should be <em><strong>true<\/strong><\/em> since, as per the spec, deriving non-virtually from <em><strong>empty<\/strong><\/em> classes makes the class <em><strong>empty<\/strong><\/em>. I believe this is a bug in Microsoft C++ compiler.<\/p>\n<p>Here are some more examples involving inheritance:<\/p>\n<figure id=\"attachment_1952\" aria-describedby=\"caption-attachment-1952\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1952\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example3-14\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg\" data-orig-size=\"594,759\" 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;1585413168&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=\"Example3\" data-image-description=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg\" class=\"size-full wp-image-1952\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg?resize=594%2C759&#038;ssl=1\" alt=\"Example3\" width=\"594\" height=\"759\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg?w=594&amp;ssl=1 594w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-1.jpg?resize=235%2C300&amp;ssl=1 235w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/a><figcaption id=\"caption-attachment-1952\" class=\"wp-caption-text\"><strong>Example: Virtual Derivation and Virtual Functions<\/strong><\/figcaption><\/figure>\n<p>These involve <em><strong>virtual<\/strong> <strong>functions<\/strong><\/em> and <em><strong>virtual<\/strong> <strong>derivation<\/strong><\/em>. What do we expect in these cases?<\/p>\n<figure id=\"attachment_1953\" aria-describedby=\"caption-attachment-1953\" style=\"width: 487px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1953\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example3-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg\" data-orig-size=\"487,187\" 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;1585413145&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=\"Example3 Output\" data-image-description=\"&lt;p&gt;Example3 Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example3 Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg\" class=\"size-full wp-image-1953\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg?resize=487%2C187&#038;ssl=1\" alt=\"Example3 Output\" width=\"487\" height=\"187\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg?w=487&amp;ssl=1 487w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example3-Output.jpg?resize=300%2C115&amp;ssl=1 300w\" sizes=\"(max-width: 487px) 100vw, 487px\" \/><\/a><figcaption id=\"caption-attachment-1953\" class=\"wp-caption-text\"><strong>Example3 Output<\/strong><\/figcaption><\/figure>\n<p>Again, there is no surprise here. If a class has a virtual function and\/or derives virtually from another class, it is <em><strong>not<\/strong><\/em> an <em><strong>empty<\/strong><\/em> class as per the spec.<\/p>\n<p>Before concluding, let us consider the case of a <em><strong>union<\/strong><\/em>\u00a0and a class with <em><strong>bit field<\/strong><\/em>:<\/p>\n<figure id=\"attachment_1954\" aria-describedby=\"caption-attachment-1954\" style=\"width: 531px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1954\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example4-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg\" data-orig-size=\"531,403\" 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;1585414704&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=\"Example4: Union and Bit Field\" data-image-description=\"&lt;p&gt;Example4: Union and Bit Field&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example4: Union and Bit Field&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg\" class=\"size-full wp-image-1954\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=531%2C403&#038;ssl=1\" alt=\"Example4: Union and Bit Field\" width=\"531\" height=\"403\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?w=531&amp;ssl=1 531w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=300%2C228&amp;ssl=1 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/a><figcaption id=\"caption-attachment-1954\" class=\"wp-caption-text\"><strong>Example4: Union and Bit Field<\/strong><\/figcaption><\/figure>\n<p>This is the output generated:<\/p>\n<figure id=\"attachment_1955\" aria-describedby=\"caption-attachment-1955\" style=\"width: 454px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1955\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/example4-output\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg\" data-orig-size=\"454,92\" 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;1585414735&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=\"Example4 Output\" data-image-description=\"&lt;p&gt;Example4 Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example4 Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg\" class=\"size-full wp-image-1955\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg?resize=454%2C92&#038;ssl=1\" alt=\"Example4 Output\" width=\"454\" height=\"92\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg?w=454&amp;ssl=1 454w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-Output.jpg?resize=300%2C61&amp;ssl=1 300w\" sizes=\"(max-width: 454px) 100vw, 454px\" \/><\/a><figcaption id=\"caption-attachment-1955\" class=\"wp-caption-text\"><strong>Example4 Output<\/strong><\/figcaption><\/figure>\n<p>As expected, <em><strong>unions<\/strong><\/em> are non-empty. And, a class with a <em><strong>bit field of size zero<\/strong><\/em> is considered <em><strong>empty<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>I hope the above examples provide some clarity on the behavior of <em><strong>std::is_empty&lt;T&gt;<\/strong><\/em> type trait.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>For the examples, I used <em><strong>Microsoft Visual Studio 2019 (ver 16.4.5)<\/strong><\/em>. You can download the examples from <a href=\"http:\/\/www.rangakrish.com\/downloads\/IsEmptyExamples.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post, we looked at the std::is_destructible&lt;T&gt; type trait. Today, let us try to understand another type trait std::is_empty&lt;T&gt;. As per the specification, is_empty&lt;T&gt;::value will return true in the following cases: &#8211; The class\/struct has no non-static data member &#8211; The class\/struct does not define a virtual function &#8211; The class\/struct does not [&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,234,235],"class_list":["post-1946","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-stdis_empty","tag-type-trait"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-vo","jetpack-related-posts":[{"id":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":1946,"position":0},"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":2011,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/","url_meta":{"origin":1946,"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":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":1946,"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":2280,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/","url_meta":{"origin":1946,"position":3},"title":"C++20 [[nodiscard]] Attribute","author":"admin","date":"January 17, 2021","format":false,"excerpt":"[[nodiscard]]\u00a0attribute \u201cencourages\u201d the compiler to issue a warning when the return value from a function is ignored. It was introduced in C++ 17 and enhanced in C++ 20 to include a string literal that can be used as an explanation of the warning. Let us look at different cases one\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Case-1: Enumeration","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.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":1946,"position":4},"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":[]},{"id":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":1946,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1946","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=1946"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1946\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}