{"id":700,"date":"2017-11-21T12:46:13","date_gmt":"2017-11-21T12:46:13","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=700"},"modified":"2017-11-21T13:30:02","modified_gmt":"2017-11-21T13:30:02","slug":"c17-stdoptional","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","title":{"rendered":"C++17: std::optional<>"},"content":{"rendered":"<p>Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This <em><strong>failure<\/strong><\/em> can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, <em><strong>-1<\/strong><\/em>). Throwing an exception is a strong form of failure and might not be appropriate in certain situations. Take the example of a lookup table that maps strings to integers. If the given key is in the table, then we can return the corresponding integer value. But what if the string is not in the table? This could be a common scenario in the application domain, and hence throwing an exception is too costly, besides complicating the processing logic. On the other hand, we cannot return something like <em><strong>-1<\/strong><\/em> to indicate failure because the value associatd with the key could be an arbitrary integer! Of course, we can introduce an extra function argument to denote the status code, but this is not elegant. What we need in this situation is the ability to return a valid value, or to return something that denotes <em><strong>no value<\/strong><\/em>. <em><strong>std::optional&lt;T&gt;<\/strong><\/em> proves handy in such situations.<\/p>\n<p>Consider the following example:<\/p>\n<figure id=\"attachment_714\" aria-describedby=\"caption-attachment-714\" style=\"width: 538px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"714\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/example1-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png\" data-orig-size=\"538,410\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example1\" data-image-description=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png\" class=\"size-full wp-image-714\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=538%2C410\" alt=\"Example1\" width=\"538\" height=\"410\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?w=538&amp;ssl=1 538w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=300%2C229&amp;ssl=1 300w\" sizes=\"(max-width: 538px) 100vw, 538px\" \/><\/a><figcaption id=\"caption-attachment-714\" class=\"wp-caption-text\"><strong>Example1<\/strong><\/figcaption><\/figure>\n<p>Here we define an <em><strong>optional<\/strong><\/em> variable to hold <em><strong>integers<\/strong><\/em>. This means the variable can, at any time, contain an integer value, or no value at all. As you can see from the variable definition, because there is no initial value for this variable, it is deemed to contain <em><strong>no value<\/strong> <\/em>to start with.<\/p>\n<p>How can we find out if the variable contains a value or not? There is a member function called <em><strong>has_value()<\/strong><\/em> that returns <b><i>true <\/i><\/b>if it has a value, or <b><i>false <\/i><\/b>if it doesn&#8217;t. Or, because there is an implicit conversion to <em><strong>bool<\/strong><\/em>, we can directly use the variable in a conditional expression.<\/p>\n<p>As in the case of regular variables, we can assign a value to the optional variable and replace its current value, if any. So, in the above example, only the third conditional check prints the value.<\/p>\n<p>Can we use <em><strong>optional<\/strong><\/em> with user-defined types? Yes, of course. Let us consider a simple <em><strong>struct X<\/strong><\/em> shown in the following figure:<\/p>\n<figure id=\"attachment_702\" aria-describedby=\"caption-attachment-702\" style=\"width: 403px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"702\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/struct-x-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png\" data-orig-size=\"403,114\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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-defined Type\" data-image-description=\"&lt;p&gt;User-defined Type&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;User-defined Type&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png\" class=\"size-full wp-image-702\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png?resize=403%2C114\" alt=\"User-defined Type\" width=\"403\" height=\"114\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png?w=403&amp;ssl=1 403w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png?resize=300%2C85&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x-1.png?resize=400%2C114&amp;ssl=1 400w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/a><figcaption id=\"caption-attachment-702\" class=\"wp-caption-text\"><strong>User-defined Type<\/strong><\/figcaption><\/figure>\n<p>The code fragment below shows how this is used with <em><strong>optional<\/strong><\/em>.<\/p>\n<figure id=\"attachment_703\" aria-describedby=\"caption-attachment-703\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"703\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/example2-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png\" data-orig-size=\"460,429\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example2\" data-image-description=\"&lt;p&gt;Example2&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example2&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png\" class=\"size-full wp-image-703\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png?resize=460%2C429\" alt=\"Example2\" width=\"460\" height=\"429\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png?w=460&amp;ssl=1 460w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2-1.png?resize=300%2C280&amp;ssl=1 300w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/a><figcaption id=\"caption-attachment-703\" class=\"wp-caption-text\"><strong>Example2<\/strong><\/figcaption><\/figure>\n<p>As in the first example, because the optional variable is defined using default initialization, the variable is deemed to be empty. However, when we provide an explicit initial value for the variable <em><strong>opt_val2<\/strong><\/em>, the optional holds that value.<\/p>\n<p>The next example shows different ways to get the value of the optional variable. We can use the <em><strong>value()<\/strong><\/em> member function, use direct assignment, or use the smart pointer dereferencing mechanism.<\/p>\n<figure id=\"attachment_704\" aria-describedby=\"caption-attachment-704\" style=\"width: 375px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"704\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/example3-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png\" data-orig-size=\"375,163\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example3\" data-image-description=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png\" class=\"size-full wp-image-704\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png?resize=375%2C163\" alt=\"Example3\" width=\"375\" height=\"163\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png?w=375&amp;ssl=1 375w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3-1.png?resize=300%2C130&amp;ssl=1 300w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/a><figcaption id=\"caption-attachment-704\" class=\"wp-caption-text\"><strong>Example3<\/strong><\/figcaption><\/figure>\n<p>Recall our initial discussion about a function returning a valid value or no value at all. To simplify the usage of such functions, there is a member function called <em><strong>value_or()<\/strong> <\/em>that allows us to supply a default value to use in case the function returns no value. This is illustratd in the following example:<\/p>\n<figure id=\"attachment_705\" aria-describedby=\"caption-attachment-705\" style=\"width: 619px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"705\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/example4-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png\" data-orig-size=\"619,328\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example4\" data-image-description=\"&lt;p&gt;Example4&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example4&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png\" class=\"size-full wp-image-705\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png?resize=619%2C328\" alt=\"Example4\" width=\"619\" height=\"328\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png?w=619&amp;ssl=1 619w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4-1.png?resize=300%2C159&amp;ssl=1 300w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/a><figcaption id=\"caption-attachment-705\" class=\"wp-caption-text\"><strong>Example4<\/strong><\/figcaption><\/figure>\n<p>Notice how the <b><i>getValue(string)<\/i><\/b> function returns <em><strong>std::nullopt<\/strong><\/em> to denote no value. The next code fragment uses <em><strong>value_or()<\/strong><\/em> to return <em><strong>9999<\/strong><\/em> if getValue() happens to return no value. This allows us to write much cleaner code.<\/p>\n<p>What if we want an optional variable that currently holds a value to become empty (i.e. no value)? The <b><i>reset() <\/i><\/b>member function does just that. This function <em><strong>destroys<\/strong><\/em> the value in the optional (calls destructor for user defined types). This is shown in the example below:<\/p>\n<figure id=\"attachment_706\" aria-describedby=\"caption-attachment-706\" style=\"width: 406px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"706\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/example5-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png\" data-orig-size=\"406,269\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&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=\"Example5\" data-image-description=\"&lt;p&gt;Example5&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example5&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png\" class=\"size-full wp-image-706\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png?resize=406%2C269\" alt=\"Example5\" width=\"406\" height=\"269\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png?w=406&amp;ssl=1 406w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5-1.png?resize=300%2C199&amp;ssl=1 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><figcaption id=\"caption-attachment-706\" class=\"wp-caption-text\"><strong>Example5<\/strong><\/figcaption><\/figure>\n<p>Once reset, the optional has no value as the example illustrates.<\/p>\n<p>In summary, <em><strong>std::optional&lt;&gt;<\/strong><\/em> gives us an elegant (and efficient) construct to represent the mutually exclusive <em><strong>value\/no value<\/strong><\/em> phenomenon.<\/p>\n<p>By the way, could we have used <em><strong>std::tuple&lt;&gt;<\/strong><\/em>, <em><strong>std::pair&lt;&gt;<\/strong><\/em>, or <em><strong>std::variant&lt;&gt;<\/strong><\/em> to model this situation? Think about it. More on this in a future article!<\/p>\n<p>The example source code is <a href=\"http:\/\/www.rangakrish.com\/downloads\/Optional Example.cpp\" target=\"_blank\" rel=\"noopener\">here.<\/a> This has been tested in Visual Studio 2017.<\/p>\n<p>Have a wonderful day!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This failure can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, -1). Throwing an exception is a strong form of failure and might not be appropriate in [&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":false,"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":[118,122,123],"class_list":["post-700","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c17","tag-stdoptional","tag-visual-studio-2017"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-bi","jetpack-related-posts":[{"id":3106,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","url_meta":{"origin":700,"position":0},"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":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":700,"position":1},"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":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":700,"position":2},"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":2280,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/","url_meta":{"origin":700,"position":3},"title":"C++20 [[nodiscard]] Attribute","author":"admin","date":"January 17, 2021","format":false,"excerpt":"[[nodiscard]]\u00a0attribute \u201cencourages\u201d the compiler to issue a warning when the return value from a function is ignored. It was introduced in C++ 17 and enhanced in C++ 20 to include a string literal that can be used as an explanation of the warning. Let us look at different cases one\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Case-1: Enumeration","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":700,"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":198,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/08\/c14-return-type-deduction-for-normal-functions\/","url_meta":{"origin":700,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/700","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=700"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/700\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}