{"id":2024,"date":"2020-06-21T08:36:01","date_gmt":"2020-06-21T03:06:01","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2024"},"modified":"2020-06-21T08:36:01","modified_gmt":"2020-06-21T03:06:01","slug":"stdis_standard_layout","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","title":{"rendered":"std::is_standard_layout<>"},"content":{"rendered":"<p>In our <a href=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/\" target=\"_blank\" rel=\"noopener noreferrer\"><em><strong>last post<\/strong><\/em><\/a>, we learnt about the type trait <em><strong>std::is_trivial&lt;T&gt;<\/strong><\/em>. Today, let us go through another type trait that is quite similar.<\/p>\n<p>The expression\u00a0<em><strong>is_standard_layout&lt;T&gt;::value<\/strong><\/em> returns <em><strong>true<\/strong><\/em> if the layout of objects of type <em><strong>T<\/strong><\/em> is compiler independent, and hence is of <em><strong>standard<\/strong><\/em> format. Else, it returns <em><strong>false<\/strong><\/em>. This is important if we create objects in <em><strong>C++<\/strong><\/em> and pass them to another language environment that is compatible with plain <em><strong>C<\/strong><\/em> structures. You might be aware that if we define a class with virtual functions, or one that derives from multiple base classes, the memory occupied by an object of that type is not guaranteed to have a <em><strong>standard<\/strong><\/em> structure.<span class=\"Apple-converted-space\">\u00a0 <\/span>For example, it might be impossible to copy of objects such a type using the C function <em><strong>memcpy<\/strong><\/em>, as the elements might not be contiguous.<\/p>\n<p>Let us look at various examples that use <em><strong>is_standard_layout&lt;T&gt;\u00a0<\/strong><\/em>and finally conclude by identifying the difference between this and <em><strong>is_trivial&lt;T&gt;<\/strong><\/em>.<\/p>\n<h3>Example-1: Primitive Types<\/h3>\n<p>This trait can be applied to primitive types as well. Here is the example program:<\/p>\n<figure id=\"attachment_2025\" aria-describedby=\"caption-attachment-2025\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2025\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/example5-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg\" data-orig-size=\"611,497\" 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;1592725333&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=\"Working with Primitive Types\" data-image-description=\"&lt;p&gt;Working with Primitive Types&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Working with Primitive Types&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg\" class=\"wp-image-2025\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg?resize=500%2C407&#038;ssl=1\" alt=\"Working with Primitive Types\" width=\"500\" height=\"407\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg?w=611&amp;ssl=1 611w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example5.jpg?resize=300%2C244&amp;ssl=1 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2025\" class=\"wp-caption-text\"><strong>Working with Primitive Types<\/strong><\/figcaption><\/figure>\n<p>And here is the output from the program:<\/p>\n<figure id=\"attachment_2026\" aria-describedby=\"caption-attachment-2026\" style=\"width: 322px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2026\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/output5-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg\" data-orig-size=\"322,175\" 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;1592725249&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=\"The Output\" data-image-description=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg\" class=\"size-full wp-image-2026\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg?resize=322%2C175&#038;ssl=1\" alt=\"The Output\" width=\"322\" height=\"175\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg?w=322&amp;ssl=1 322w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output5.jpg?resize=300%2C163&amp;ssl=1 300w\" sizes=\"(max-width: 322px) 100vw, 322px\" \/><\/a><figcaption id=\"caption-attachment-2026\" class=\"wp-caption-text\"><strong>The Output<\/strong><\/figcaption><\/figure>\n<h3>Example-2: Simple Classes<\/h3>\n<p>Take a look at the structs <em><strong>A, B, C, <\/strong><\/em>and<em><strong> D\u00a0<\/strong><\/em>below.<\/p>\n<figure id=\"attachment_2027\" aria-describedby=\"caption-attachment-2027\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2027\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/example1-20\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg\" data-orig-size=\"681,1337\" 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;1592668623&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=\"Basic Classes\" data-image-description=\"&lt;p&gt;Basic Classes&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Basic Classes&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1-522x1024.jpg\" class=\"size-large wp-image-2027\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg?resize=522%2C1024&#038;ssl=1\" alt=\"Basic Classes\" width=\"522\" height=\"1024\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg?resize=522%2C1024&amp;ssl=1 522w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg?resize=153%2C300&amp;ssl=1 153w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example1-1.jpg?w=681&amp;ssl=1 681w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/><\/a><figcaption id=\"caption-attachment-2027\" class=\"wp-caption-text\"><strong>Basic Classes<\/strong><\/figcaption><\/figure>\n<p>The struct <em><strong>A<\/strong><\/em> is a typical <em><strong>POD (Plain Old Data)<\/strong><\/em>, containing only data members. Struct <em><strong>B<\/strong><\/em> includes a static member, but otherwise it is quite similar to <em><strong>A<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Struct <em><strong>C<\/strong><\/em> contains a no-arg constructor, a copy constructor, destructor, and a member function. Struct <em><strong>D<\/strong><\/em> is different in that it enforces access control.<\/p>\n<p>Let us see the output of the program:<\/p>\n<figure id=\"attachment_2029\" aria-describedby=\"caption-attachment-2029\" style=\"width: 176px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output1-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2029\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/output1-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output1-1.jpg\" data-orig-size=\"176,102\" 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;1592668583&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=\"Program Outpurt\" data-image-description=\"&lt;p&gt;Program Outpurt&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Outpurt&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output1-1.jpg\" class=\"size-full wp-image-2029\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output1-1.jpg?resize=176%2C102&#038;ssl=1\" alt=\"Program Outpurt\" width=\"176\" height=\"102\" \/><\/a><figcaption id=\"caption-attachment-2029\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Notice that, even though <em><strong>D<\/strong><\/em>&#8216;s members are not public, it still has a standard layout.<\/p>\n<h3>Example-3: Special Cases<\/h3>\n<p>Let us extend the earlier example by including two special cases:<\/p>\n<p style=\"padding-left: 40px;\">1) Class has reference member variable<\/p>\n<p style=\"padding-left: 40px;\">2) Class defines more than one access level<\/p>\n<p>Here is the example:<\/p>\n<figure id=\"attachment_2030\" aria-describedby=\"caption-attachment-2030\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2030\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/example2-16\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg\" data-orig-size=\"703,807\" 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;1592668836&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=\"Special Cases\" data-image-description=\"&lt;p&gt;Special Cases&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Special Cases&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg\" class=\"wp-image-2030\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg?resize=550%2C631&#038;ssl=1\" alt=\"Special Cases\" width=\"550\" height=\"631\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg?w=703&amp;ssl=1 703w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example2.jpg?resize=261%2C300&amp;ssl=1 261w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2030\" class=\"wp-caption-text\"><strong>Special Cases<\/strong><\/figcaption><\/figure>\n<p>Here is the program&#8217;s output:<\/p>\n<figure id=\"attachment_2031\" aria-describedby=\"caption-attachment-2031\" style=\"width: 205px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output2-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2031\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/output2-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output2-1.jpg\" data-orig-size=\"205,52\" 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;1592668776&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output2-1.jpg\" class=\"size-full wp-image-2031\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output2-1.jpg?resize=205%2C52&#038;ssl=1\" alt=\"Program Output\" width=\"205\" height=\"52\" \/><\/a><figcaption id=\"caption-attachment-2031\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>As you can see, both the types have non-standard layout.<\/p>\n<h3>Example-4: Class Hierarchy<\/h3>\n<p>This section covers classes that have virtual functions or derive from other classes. Go through the example:<\/p>\n<figure id=\"attachment_2032\" aria-describedby=\"caption-attachment-2032\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2032\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/example3-16\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg\" data-orig-size=\"655,1242\" 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;1592669074&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=\"Class Hierarchy\" data-image-description=\"&lt;p&gt;Class Hierarchy&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Class Hierarchy&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1-540x1024.jpg\" class=\"wp-image-2032\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg?resize=500%2C948&#038;ssl=1\" alt=\"Class Hierarchy\" width=\"500\" height=\"948\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg?resize=540%2C1024&amp;ssl=1 540w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg?resize=158%2C300&amp;ssl=1 158w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example3-1.jpg?w=655&amp;ssl=1 655w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2032\" class=\"wp-caption-text\"><strong>Class Hierarchy<\/strong><\/figcaption><\/figure>\n<p>Struct <em><strong>G<\/strong><\/em> has a virtual destructor. We know that objects of such a class need additional storage for keeping track of the virtual table, something that is not the case with a simple struct acting like a <em><strong>POD<\/strong><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Struct <em><strong>I<\/strong><\/em> uses private derivation while inheriting from <em><strong>C<\/strong><\/em>.<\/p>\n<p>Struct <em><strong>J<\/strong><\/em> differs from <em><strong>H<\/strong><\/em> in that it defines an additional data member in its scope. Struct <em><strong>K<\/strong><\/em> inherits from two different base classes.<\/p>\n<p>Can you guess the output? Here it is:<\/p>\n<figure id=\"attachment_2033\" aria-describedby=\"caption-attachment-2033\" style=\"width: 166px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output3-1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2033\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/output3-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output3-1.jpg\" data-orig-size=\"166,126\" 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;1592669007&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output3-1.jpg\" class=\"size-full wp-image-2033\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output3-1.jpg?resize=166%2C126&#038;ssl=1\" alt=\"Program Output\" width=\"166\" height=\"126\" \/><\/a><figcaption id=\"caption-attachment-2033\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Interesting. Even though <em><strong>I<\/strong><\/em> derives <em><strong>privately<\/strong><\/em> from its base class, it has a standard layout.<\/p>\n<p>The above examples show that there is substantial similarity between <em><strong>is_trivial&lt;T&gt;<\/strong><\/em> and <em><strong>is_standard_layout&lt;T&gt;<\/strong><\/em>. What is the difference, if any? Well, struct <em><strong>C <\/strong><\/em>in our example<em><strong>,\u00a0<\/strong><\/em>holds the answer.<\/p>\n<figure id=\"attachment_2034\" aria-describedby=\"caption-attachment-2034\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2034\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/example4-11\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg\" data-orig-size=\"863,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;1592669274&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=\"The Difference\" data-image-description=\"&lt;p&gt;The Difference&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Difference&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg\" class=\"wp-image-2034\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=550%2C376&#038;ssl=1\" alt=\"The Difference\" width=\"550\" height=\"376\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?w=863&amp;ssl=1 863w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=300%2C205&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=768%2C525&amp;ssl=1 768w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2034\" class=\"wp-caption-text\"><strong>The Difference<\/strong><\/figcaption><\/figure>\n<p>When we run this program, this is what we get:<\/p>\n<figure id=\"attachment_2035\" aria-describedby=\"caption-attachment-2035\" style=\"width: 251px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2035\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/output4-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output4.jpg\" data-orig-size=\"301,66\" 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;1592669213&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output4.jpg\" class=\"wp-image-2035\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Output4.jpg?resize=251%2C55&#038;ssl=1\" alt=\"Program Output\" width=\"251\" height=\"55\" \/><\/a><figcaption id=\"caption-attachment-2035\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The presence of constructors and destructor makes struct <em><strong>C<\/strong><\/em> non-trivial, although it has standard layout.<\/p>\n<p>That is it for today. Hope you found the discussion useful.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Download the sources from <a href=\"http:\/\/www.rangakrish.com\/downloads\/IsStandardLayout.zip\" target=\"_blank\" rel=\"noopener noreferrer\"><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 our last post, we learnt about the type trait std::is_trivial&lt;T&gt;. Today, let us go through another type trait that is quite similar. The expression\u00a0is_standard_layout&lt;T&gt;::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 important if we create objects [&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_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},"jetpack_post_was_ever_published":false},"categories":[49,17],"tags":[67,241,235],"class_list":["post-2024","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-is_standard_layout","tag-type-trait"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-wE","jetpack-related-posts":[{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":2024,"position":0},"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":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":2024,"position":1},"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":1960,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","url_meta":{"origin":2024,"position":2},"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":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":2024,"position":3},"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":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":2024,"position":4},"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":2011,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/","url_meta":{"origin":2024,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2024","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=2024"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2024\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}