{"id":1960,"date":"2020-04-12T10:25:41","date_gmt":"2020-04-12T04:55:41","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1960"},"modified":"2020-04-13T05:05:16","modified_gmt":"2020-04-12T23:35:16","slug":"stdcommon_type-type-trait","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","title":{"rendered":"std::common_type<> Type Trait"},"content":{"rendered":"<p>In today&#8217;s post, I would like to go over the type trait <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/common_type\" target=\"_blank\" rel=\"noopener\"><em><strong>std::common_type&lt;&gt;<\/strong><\/em><\/a>. This trait was introduced in <em><strong>C++11<\/strong><\/em>.<\/p>\n<p>As per the specification, <em><strong>std::common_type&lt;T1, T2, &#8230;Tn&gt;::type<\/strong><\/em> refers to a type <em><strong>Tx<\/strong><\/em> in the given list, which the rest of the types in the list can be implicitly converted to. This works with built-in as well as user-defined types.<\/p>\n<p>In the examples below, I will be using the <em><strong>Helper<\/strong><\/em> type:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>template&lt; class&#8230; T &gt;<\/strong><\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\"><strong>using common_type_t = typename common_type&lt;T&#8230;&gt;::type;<\/strong><\/span><\/p>\n<p>This helper was introduced in <em><strong>C++14<\/strong><\/em>.<\/p>\n<p>Our first example demonstrates how this type trait works with built-in types:<\/p>\n<figure id=\"attachment_1961\" aria-describedby=\"caption-attachment-1961\" style=\"width: 578px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1961\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/fig1-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg\" data-orig-size=\"578,569\" 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;1586680848&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=\"Common_type Trait with Built-in Types\" data-image-description=\"&lt;p&gt;Common_type Trait with Built-in Types&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;CommonType Trait with Built-in Types&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg\" class=\"size-full wp-image-1961\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg?resize=578%2C569&#038;ssl=1\" alt=\"Common_type Trait with Built-in Types\" width=\"578\" height=\"569\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg?w=578&amp;ssl=1 578w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig1.jpg?resize=300%2C295&amp;ssl=1 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/a><figcaption id=\"caption-attachment-1961\" class=\"wp-caption-text\"><strong>Common_type Trait with Built-in Types<\/strong><\/figcaption><\/figure>\n<p><em><strong>Line 13<\/strong><\/em> defines a variable <em><strong>var1<\/strong><\/em>, whose type will be the common type of the three types <em><strong>&#8220;int&#8221;, &#8220;float&#8221; <\/strong><\/em>and<em><strong> &#8220;double&#8221;<\/strong><\/em>. As you might have guessed correctly, the common type happens to be <em><strong>&#8220;double&#8221;<\/strong><\/em>. This is verified in <em><strong>Line 14<\/strong><\/em>, where we print the name of the type (please note that the exact name printed is compiler specific, but it should uniquely denote the type). The actual output is given in the following figure:<\/p>\n<figure id=\"attachment_1962\" aria-describedby=\"caption-attachment-1962\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1962\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/fig2-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig2.jpg\" data-orig-size=\"712,71\" 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;1586680890&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\/04\/Fig2.jpg\" class=\"wp-image-1962\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig2.jpg?resize=650%2C65&#038;ssl=1\" alt=\"Program Output\" width=\"650\" height=\"65\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig2.jpg?w=712&amp;ssl=1 712w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig2.jpg?resize=300%2C30&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1962\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>What about <em><strong>var2<\/strong><\/em> in <em><strong>Line 16<\/strong><\/em>? Since a <em><strong>&#8220;char&#8221;<\/strong><\/em> can be implicitly converted to an <em><strong>&#8220;int&#8221;<\/strong><\/em>, but not vice versa, <em><strong>&#8220;int&#8221;<\/strong><\/em> happens to be the common type here.<\/p>\n<p>We have a problem with <em><strong>var3<\/strong><\/em> in <em><strong>Line 19<\/strong><\/em>. There is no implicit conversion between <em><strong>&#8220;const char *&#8221;<\/strong><\/em> and either <em><strong>&#8220;int&#8221;<\/strong><\/em> or <em><strong>&#8220;char&#8221;<\/strong><\/em>. Hence this is a compile-time error.<\/p>\n<p><em><strong>var4<\/strong><\/em> on <em><strong>Line 22<\/strong><\/em> will have the type <em><strong>&#8220;string&#8221;<\/strong><\/em> since <em><strong>&#8220;char *&#8221;<\/strong><\/em> can be implicitly converted to <em><strong>&#8220;string&#8221;<\/strong><\/em>.<\/p>\n<p><em><strong>var5<\/strong><\/em> on <em><strong>Line 25<\/strong><\/em> illustrates another trivial case since <em><strong>&#8220;void&#8221;<\/strong><\/em> is incompatible with any other type.<\/p>\n<p>A slight variation of the above is <em><strong>var6<\/strong><\/em>. In this case, since pointer to any given type (except pointer to members) can be implicitly converted to <strong>&#8220;void *&#8221;<\/strong>, the type of <em><strong>var6<\/strong><\/em> will be <em><strong>&#8220;void *&#8221;<\/strong><\/em> and the output confirms this.<\/p>\n<p>Let us now consider some user-defined types. See the figure below.<\/p>\n<figure id=\"attachment_1963\" aria-describedby=\"caption-attachment-1963\" style=\"width: 543px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1963\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/fig3-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg\" data-orig-size=\"543,506\" 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;1586679093&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=\"User Types\" data-image-description=\"&lt;p&gt;User Types&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;User Types&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg\" class=\"size-full wp-image-1963\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg?resize=543%2C506&#038;ssl=1\" alt=\"User Types\" width=\"543\" height=\"506\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg?w=543&amp;ssl=1 543w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig3.jpg?resize=300%2C280&amp;ssl=1 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/a><figcaption id=\"caption-attachment-1963\" class=\"wp-caption-text\"><strong>User Types<\/strong><\/figcaption><\/figure>\n<p>There is nothing complex here. The <em><strong>&#8220;<\/strong><strong>Root&#8221;<\/strong><\/em> class serves as the base class for all the other classes. Classes <em><strong>&#8220;A&#8221;<\/strong><\/em> and <em><strong>&#8220;B&#8221;<\/strong><\/em> derive virtually from <em><strong>&#8220;Root&#8221;<\/strong><\/em> and because of this, class <em><strong>&#8220;E&#8221;<\/strong><\/em> (<em><strong>Line 29<\/strong><\/em>) gets a single instance of <em><strong>&#8220;Root&#8221;<\/strong><\/em> via both <em><strong>&#8220;A&#8221;<\/strong><\/em> and <em><strong>&#8220;B&#8221;<\/strong><\/em>.<\/p>\n<p>Here is the code fragment showing the usage of <em><strong>common_type&lt;&gt;<\/strong><\/em> in the context of this class hierarchy.<\/p>\n<figure id=\"attachment_1964\" aria-describedby=\"caption-attachment-1964\" style=\"width: 627px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1964\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/fig4-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg\" data-orig-size=\"627,889\" 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;1586679132&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=\"Example Code\" data-image-description=\"&lt;p&gt;Example Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg\" class=\"size-full wp-image-1964\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg?resize=627%2C889&#038;ssl=1\" alt=\"Example Code\" width=\"627\" height=\"889\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg?w=627&amp;ssl=1 627w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig4.jpg?resize=212%2C300&amp;ssl=1 212w\" sizes=\"(max-width: 627px) 100vw, 627px\" \/><\/a><figcaption id=\"caption-attachment-1964\" class=\"wp-caption-text\"><em><strong>Example Code<\/strong><\/em><\/figcaption><\/figure>\n<p>In <em><strong>Line 35<\/strong><\/em>, we define variables to hold the different user types.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p><em><strong>Line 38<\/strong><\/em> defines a convenient <em><strong>lambda<\/strong><\/em> function to print the name of the class corresponding to a given object.<\/p>\n<p><em><strong>Line 44<\/strong><\/em>: Since <em><strong>&#8220;C&#8221;<\/strong><\/em> is derived from <em><strong>&#8220;A&#8221;<\/strong><\/em>, <em><strong>&#8220;A&#8221;<\/strong><\/em> will be the <em><strong>common type<\/strong><\/em> here. Hence we can reference objects of type both <em><strong>&#8220;A&#8221;<\/strong><\/em> and <em><strong>&#8220;C&#8221;<\/strong><\/em> using this type.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>The output from the sample program is here:<\/p>\n<figure id=\"attachment_1966\" aria-describedby=\"caption-attachment-1966\" style=\"width: 377px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1966\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/fig5-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg\" data-orig-size=\"377,217\" 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;1586679170&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\/04\/Fig5.jpg\" class=\"size-full wp-image-1966\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?resize=377%2C217&#038;ssl=1\" alt=\"Program Output\" width=\"377\" height=\"217\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?w=377&amp;ssl=1 377w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?resize=300%2C173&amp;ssl=1 300w\" sizes=\"(max-width: 377px) 100vw, 377px\" \/><\/a><figcaption id=\"caption-attachment-1966\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p><em><strong>Line 48<\/strong><\/em>: <em><strong>&#8220;Root&#8221;<\/strong><\/em> is the virtual base class of <em><strong>&#8220;C&#8221;<\/strong><\/em> through <em><strong>&#8220;A&#8221;<\/strong><\/em>. Hence that is also the common type in this case.<\/p>\n<p><em><strong>Line 52<\/strong><\/em>: Since <em><strong>&#8220;B&#8221;<\/strong><\/em> is a direct base class of <em><strong>&#8220;E&#8221;<\/strong><\/em>, the common type here will be <em><strong>&#8220;B&#8221;<\/strong><\/em>.<\/p>\n<p><em><strong>Line 56<\/strong><\/em>: There should be no surprise here. <em><strong>&#8220;Root&#8221;<\/strong><\/em> is the common type.<\/p>\n<p><em><strong>Line 64<\/strong><\/em> uses only one type, which is permitted. The common type is, trivially, that single type\u00a0<em><strong>&#8220;D&#8221;<\/strong><\/em>.<\/p>\n<p><em><strong>Lines 69, 70 <\/strong><\/em>and<em><strong> 71<\/strong><\/em> show an important property of <em><strong>common_type<\/strong><\/em> trait. Although the pairs <em><strong>&lt;A, B&gt;, &lt;C, D&gt; <\/strong><\/em>and<em><strong> &lt;D, E&gt;<\/strong><\/em> all share a common base class <em><strong>&#8220;Root&#8221;<\/strong><\/em>, that type is not identified as the common type through <em><strong>inference<\/strong><\/em>. The type trait only determines a common type, if possible, from among the listed types. This could come as a surprise to a few (it did to me initially!), but that is the reality.<\/p>\n<p>Hope today&#8217;s discussion throws sufficient clarity on this type trait. You can download the example code from <a href=\"http:\/\/www.rangakrish.com\/downloads\/CommonTypeExamples.zip\" target=\"_blank\" rel=\"noopener\"><em><strong>here.<\/strong><\/em><\/a><\/p>\n<p>Have a nice weeekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s post, I would like to go over the type trait std::common_type&lt;&gt;. This trait was introduced in C++11. As per the specification, std::common_type&lt;T1, T2, &#8230;Tn&gt;::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 works with built-in as well [&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,236,235],"class_list":["post-1960","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-common_type","tag-type-trait"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-vC","jetpack-related-posts":[{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":1960,"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":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":1960,"position":1},"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":1960,"position":2},"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":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":1960,"position":3},"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":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":1960,"position":4},"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":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":1960,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1960","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=1960"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1960\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}