{"id":1973,"date":"2020-04-26T08:24:52","date_gmt":"2020-04-26T02:54:52","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=1973"},"modified":"2020-04-26T08:24:52","modified_gmt":"2020-04-26T02:54:52","slug":"stdis_base_of","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","title":{"rendered":"std::is_base_of<>"},"content":{"rendered":"<p>In our on-going series on C++ <em><strong>Type Traits<\/strong><\/em>, today&#8217;s topic is about the trait <em><strong>std::is_base_of&lt;&gt;<\/strong><\/em>.<\/p>\n<p>For the official description, see <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/types\/is_base_of\" target=\"_blank\" rel=\"noopener\"><em><strong>this<\/strong><\/em><\/a>.<\/p>\n<p><em><strong>std::is_base_of&lt;A, B&gt;::value<\/strong><\/em> takes two arguments, both classes (or structs), and returns <em><strong>true<\/strong><\/em> if <em><strong>A<\/strong><\/em> is a base class of <em><strong>B<\/strong><\/em> and <em><strong>false<\/strong><\/em> otherwise. Trivially, <em><strong>std::&lt;A, A&gt;::value<\/strong><\/em> is <em><strong>true<\/strong><\/em>.<\/p>\n<p>Let us look at an example covering various cases. For convenience, we will use the construct <em><strong>std::is_base_of_v&lt;T1, T2&gt;<\/strong><\/em> instead of <em><strong>std::is_base_of&lt;T1, T2&gt;::value<\/strong><\/em>.<\/p>\n<figure id=\"attachment_1974\" aria-describedby=\"caption-attachment-1974\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Program.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"1974\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/program-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Program.jpg\" data-orig-size=\"728,898\" 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;1587883051&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\/Program.jpg\" class=\"wp-image-1974\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Program.jpg?resize=650%2C802&#038;ssl=1\" alt=\"Example Code\" width=\"650\" height=\"802\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Program.jpg?w=728&amp;ssl=1 728w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Program.jpg?resize=243%2C300&amp;ssl=1 243w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-1974\" class=\"wp-caption-text\"><strong>Example Code<\/strong><\/figcaption><\/figure>\n<p>The corresponding output appears below.<\/p>\n<figure id=\"attachment_1975\" aria-describedby=\"caption-attachment-1975\" style=\"width: 474px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"1975\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/output-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg\" data-orig-size=\"474,266\" 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;1587883104&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=\"Output\" data-image-description=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg\" class=\"size-full wp-image-1975\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=474%2C266&#038;ssl=1\" alt=\"Output\" width=\"474\" height=\"266\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?w=474&amp;ssl=1 474w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=352%2C198&amp;ssl=1 352w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/><\/a><figcaption id=\"caption-attachment-1975\" class=\"wp-caption-text\"><strong>Output<\/strong><\/figcaption><\/figure>\n<p>Here are some important observations:<\/p>\n<ol>\n<li>Primitive types such as <em><strong>int<\/strong><\/em> and <em><strong>double<\/strong><\/em> do not participate in <em><strong>Base\/Derived<\/strong><\/em> relationship<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<li>A class is a base of itself<\/li>\n<li>A class is considered a valid base class irrespective of whether the derivation is<em><strong> private, protected <\/strong><\/em>or<em><strong> public<\/strong><\/em><\/li>\n<li>Even if a class is included multiple times in a derived class via different paths (and hence could cause ambiguity), it is still a valid base class <span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<\/ol>\n<p>Notice that in <em><strong>line 39<\/strong><\/em>, we are using <em><strong>decltype<\/strong><\/em> to get the type of the variable <em><strong>qvar<\/strong><\/em> (it can be any expression).<\/p>\n<p>As you can see, <em><strong>std::is_base_of&lt;T1, T2&gt;<\/strong><\/em> is a simple, but nevertheless useful, type trait in C++.<\/p>\n<p>I used <em><strong>Visual Studio 2019 version 16.5.4<\/strong><\/em> to test the above code. You can download the source from <a href=\"http:\/\/www.rangakrish.com\/downloads\/IsBaseOf.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our on-going series on C++ Type Traits, today&#8217;s topic is about the trait std::is_base_of&lt;&gt;. For the official description, see this. std::is_base_of&lt;A, B&gt;::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::&lt;A, A&gt;::value is true. Let us look at an example [&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,237,235],"class_list":["post-1973","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-is_base_of","tag-type-trait"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-vP","jetpack-related-posts":[{"id":198,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/08\/c14-return-type-deduction-for-normal-functions\/","url_meta":{"origin":1973,"position":0},"title":"C++14: Return Type Deduction for Normal Functions","author":"admin","date":"April 8, 2016","format":false,"excerpt":"In C++11, a function must specify its return type in one of the following two ways: int aFunction(int arg) { \u00a0 \u00a0 return arg * 2; } (or) auto aFunction(int arg) -> int { \u00a0 \u00a0 return arg * 2; } The first is the classic function definition syntax. The\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":1973,"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":1960,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","url_meta":{"origin":1973,"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":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":1973,"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":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":1973,"position":4},"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":3106,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","url_meta":{"origin":1973,"position":5},"title":"std::expected","author":"admin","date":"May 13, 2023","format":false,"excerpt":"I had written about std::optional<> in an earlier article. C++23 introduces std::expected<> as an interesting extension to std::optional<>. Whereas std::optional<> contains a value or none at all, std::expected<> contains a value or an error code associated with it. This gives better control to the caller in terms of handling the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"An Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1973","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=1973"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/1973\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=1973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=1973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=1973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}