{"id":3230,"date":"2023-10-15T08:54:39","date_gmt":"2023-10-15T03:24:39","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3230"},"modified":"2023-10-15T08:54:39","modified_gmt":"2023-10-15T03:24:39","slug":"c20-constinit-specifier","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/","title":{"rendered":"C++20: \u201cconstinit\u201d Specifier"},"content":{"rendered":"<p>The <em><strong>constinit<\/strong><\/em> specifier, introduced in <em><strong>C++20<\/strong><\/em>, is applied to static variables (global and local static) and thread local variables, with the requirement that they either have a <em><strong>zero<\/strong><\/em> initialization or they are initialized with a compile-time constant expression.<\/p>\n<p>Here is our first example:<\/p>\n<figure id=\"attachment_3231\" aria-describedby=\"caption-attachment-3231\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3231\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/case1-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg\" data-orig-size=\"822,488\" 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;1697275008&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-1: Basic Types\" data-image-description=\"&lt;p&gt;Example-1: Basic Types&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-1: Basic Types&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg\" class=\"wp-image-3231\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg?resize=500%2C297&#038;ssl=1\" alt=\"Example-1: Basic Types\" width=\"500\" height=\"297\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg?resize=300%2C178&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg?resize=768%2C456&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1.jpg?w=822&amp;ssl=1 822w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3231\" class=\"wp-caption-text\"><strong>Example-1: Basic Types<\/strong><\/figcaption><\/figure>\n<p>Line 17 declares a global <em><strong>constinit<\/strong><\/em> variable, initialized with a constant value. Line 18 is an example of <em><strong>zero<\/strong><\/em> initialization.<\/p>\n<p>Line 27 (commented out) shows that it is illegal to declare a <em><strong>local<\/strong><\/em> constinit variable. However, we can have <em><strong>local static<\/strong><\/em> variables that are <em><strong>constinit<\/strong><\/em> qualified. This is shown in Line 28.<\/p>\n<p>Notice that <em><strong>constinit<\/strong><\/em> variables need not be <em><strong>const<\/strong><\/em>\u00a0 (immutable) throughout their lifetime. They can be modified at runtime. This is shown in line 32, where <em><strong>\u201cglobal2\u201d<\/strong><\/em> is assigned a value computed from a regular function call. This is perfectly legal.<\/p>\n<p>Here is the program output:<\/p>\n<figure id=\"attachment_3232\" aria-describedby=\"caption-attachment-3232\" style=\"width: 350px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3232\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/output1-15\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output1.jpg\" data-orig-size=\"596,139\" 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;1697274916&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\/2023\/10\/output1.jpg\" class=\"wp-image-3232\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output1.jpg?resize=350%2C82&#038;ssl=1\" alt=\"Program Output\" width=\"350\" height=\"82\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output1.jpg?resize=300%2C70&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output1.jpg?w=596&amp;ssl=1 596w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><figcaption id=\"caption-attachment-3232\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>No surprise here.<\/p>\n<p>The next example is based on an aggregate data type, a <em><strong>struct<\/strong><\/em> in this case:<\/p>\n<figure id=\"attachment_3234\" aria-describedby=\"caption-attachment-3234\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3234\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/case2-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg\" data-orig-size=\"1017,465\" 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;1697292761&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-2: Aggregate Type\" data-image-description=\"&lt;p&gt;Example-2: Aggregate Type&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-2: Aggregate Type&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg\" class=\"wp-image-3234\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg?resize=550%2C251&#038;ssl=1\" alt=\"Example-2: Aggregate Type\" width=\"550\" height=\"251\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg?resize=300%2C137&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg?resize=768%2C351&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case2.jpg?w=1017&amp;ssl=1 1017w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3234\" class=\"wp-caption-text\"><strong>Example-2: Aggregate Type<\/strong><\/figcaption><\/figure>\n<p>For the sake of illustration, I have defined three constructors, the third one being <em><strong>constexpr<\/strong><\/em> qualified. Just as in the case of a variable of a primitive type, an object of <em><strong>\u201cTemp\u201d<\/strong><\/em> struct can be declared using <em><strong>constinit<\/strong><\/em> at <em><strong>\u201cstatic\u201d<\/strong><\/em> scope, but only when the <em><strong>\u201cappropriate\u201d<\/strong><\/em> constructor is available. As lines 48-50 show, the <em><strong>constexpr<\/strong><\/em> constructor is the only one that can be used here.<\/p>\n<p>Line 54 is a reminder that local variables (even using <em><strong>constexpr<\/strong><\/em> constructor) cannot be qualified <em><strong>constinit<\/strong><\/em>. As in the earlier example, in line 55 we modify the state of the <em><strong>constinit<\/strong><\/em> qualified variable at runtime. This is what the program prints:<\/p>\n<figure id=\"attachment_3235\" aria-describedby=\"caption-attachment-3235\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3235\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/output2-17\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output2.jpg\" data-orig-size=\"637,109\" 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;1697275088&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\/2023\/10\/output2.jpg\" class=\"wp-image-3235\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output2.jpg?resize=400%2C68&#038;ssl=1\" alt=\"Program Output\" width=\"400\" height=\"68\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output2.jpg?resize=300%2C51&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output2.jpg?w=637&amp;ssl=1 637w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3235\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Our final example shows the use of <em><strong>constinit<\/strong><\/em> applied to <em><strong>\u201cthread local\u201d<\/strong><\/em> variables:<\/p>\n<figure id=\"attachment_3236\" aria-describedby=\"caption-attachment-3236\" style=\"width: 599px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3236\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/case3-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg\" data-orig-size=\"1296,640\" 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;1697275265&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-: Thread Local Variables\" data-image-description=\"&lt;p&gt;Example-: Thread Local Variables&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-: Thread Local Variables&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3-1024x506.jpg\" class=\"wp-image-3236\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?resize=599%2C296&#038;ssl=1\" alt=\"Example-: Thread Local Variables\" width=\"599\" height=\"296\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?resize=300%2C148&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?resize=1024%2C506&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?resize=768%2C379&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case3.jpg?w=1296&amp;ssl=1 1296w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><figcaption id=\"caption-attachment-3236\" class=\"wp-caption-text\"><strong>Example-3: Thread Local Variables<\/strong><\/figcaption><\/figure>\n<p>Here we have a global <em><strong>thread_local<\/strong><\/em> variable as well as a local static (declared inside <em><strong>thread_fn()<\/strong><\/em>), both of them declared with <em><strong>constinit<\/strong><\/em> qualifier. Each instance of <em><strong>thread_fn()<\/strong><\/em> increments these two variables and prints on standard output. The <em><strong>mutex<\/strong><\/em> is used to regulate access to <em><strong>\u201cstd::cout\u201d<\/strong><\/em> instance.<\/p>\n<p>Here is the output I get when I execute the program:<\/p>\n<figure id=\"attachment_3237\" aria-describedby=\"caption-attachment-3237\" style=\"width: 350px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3237\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/output3-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg\" data-orig-size=\"594,329\" 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;1697275231&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=\"Multithreading Output\" data-image-description=\"&lt;p&gt;Multithreading Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Multithreading Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg\" class=\"wp-image-3237\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg?resize=350%2C194&#038;ssl=1\" alt=\"Multithreading Output\" width=\"350\" height=\"194\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg?resize=300%2C166&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/output3.jpg?w=594&amp;ssl=1 594w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><figcaption id=\"caption-attachment-3237\" class=\"wp-caption-text\"><strong>Multithreading Output<\/strong><\/figcaption><\/figure>\n<p>I could have added more threads to make the behavior more interesting, but I hope you get the idea.<\/p>\n<p>One last thing before I conclude this article. <em><strong>constinit<\/strong><\/em> variables defined across multiple source files can be declared <em><strong>\u201cextern\u201d<\/strong> <\/em>where needed, for example:<\/p>\n<p style=\"padding-left: 40px;\"><em><strong>extern constinit int global1;<\/strong><\/em><\/p>\n<p>Since the variable is assumed to have been defined in some module, initialization is not required here.<\/p>\n<p>I tested this code in <em><strong>Visual Studio Professional 2022 (64 it) Ver 17.7.5<\/strong><\/em>. You can download the source file <a href=\"https:\/\/www.rangakrish.com\/downloads\/constinit.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The constinit specifier, introduced in C++20, is applied to static variables (global and local static) and thread local variables, with the requirement that they either have a zero initialization or they are initialized with a compile-time constant expression. Here is our first example: Line 17 declares a global constinit variable, initialized with a constant value. [&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":[371,372],"class_list":["post-3230","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c20","tag-constinit"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Q6","jetpack-related-posts":[{"id":3249,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/06\/book-review-c-initialization-story\/","url_meta":{"origin":3230,"position":0},"title":"Book Review: C++ Initialization Story","author":"admin","date":"November 6, 2023","format":false,"excerpt":"Title: C++ Initialization Story - A Guide Through All Initialization Options and Related C++ Areas Author: Bart\u0142omiej Filipek Publisher: Leanpub Year: 2023 Can you believe that there is a language where \u201cinitialization\u201d itself involves many subtleties and a whole book can be devoted to this topic? Yes, modern C++ is\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Book-cover-231x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1339,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/12\/23\/book-review-real-time-c\/","url_meta":{"origin":3230,"position":1},"title":"Book Review: Real-Time C++","author":"admin","date":"December 23, 2018","format":false,"excerpt":"Title: Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming Author: Christopher Kormanyos Publisher: Springer-Verlag Year: 2018 (Third Edition) It was the title of this book which caught my attention a few weeks ago and I decided to invest in it. I am not in to real-time software development these days,\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":3230,"position":2},"title":"C++17: std::variant","author":"admin","date":"November 5, 2017","format":false,"excerpt":"C++17 introduces a new type-safe union in the form of std::variant. At any time, it can store a single value from one of many types. We need to include <variant>\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":3230,"position":3},"title":"C++17: Initialization in Selection Statements","author":"admin","date":"October 21, 2017","format":false,"excerpt":"C++17 enhances if and switch statements with the ability to define\u00a0 variables whose life-time is limited to the corresponding scope. This is in keeping with the general guideline that variables should have a tight scope, i.e.,\u00a0 should be defined as close to the point of use as possible and should\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Sample Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/classdefn.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":3230,"position":4},"title":"C++17: std::any","author":"admin","date":"December 3, 2017","format":false,"excerpt":"In the previous two posts, I talked about std::variant<> and std::optional<>. Today, I want to take up std::any for discussion. The type any (implemented by the class any) allows a variable to hold a single value of any type. More interestingly, the type of the value held by a variable\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2011,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/06\/stdis_trivial\/","url_meta":{"origin":3230,"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\/3230","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=3230"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3230\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}