{"id":3106,"date":"2023-05-13T10:42:25","date_gmt":"2023-05-13T05:12:25","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3106"},"modified":"2023-05-13T10:42:25","modified_gmt":"2023-05-13T05:12:25","slug":"stdexpected","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","title":{"rendered":"std::expected<>"},"content":{"rendered":"<p>I had written about <a href=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/\" target=\"_blank\" rel=\"noopener\"><em><strong>std::optional&lt;&gt;<\/strong><\/em><\/a> in an earlier article. <em><strong>C++23<\/strong><\/em> introduces <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/expected\" target=\"_blank\" rel=\"noopener\"><em><strong>std::expected&lt;&gt;<\/strong><\/em><\/a> as an interesting extension to <em><strong>std::optional&lt;&gt;<\/strong><\/em>. Whereas <em><strong>std::optional&lt;&gt;<\/strong><\/em> contains a value or none at all, <em><strong>std::expected&lt;&gt;<\/strong><\/em> contains a <em><strong>value<\/strong><\/em> or an <em><strong>error code<\/strong><\/em> associated with it. This gives better control to the caller in terms of handling the outcome of the function call.<br \/>\nLet us look at an example to get started:<\/p>\n<figure id=\"attachment_3108\" aria-describedby=\"caption-attachment-3108\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3108\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/source1\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg\" data-orig-size=\"948,838\" 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;1683813088&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=\"An Example\" data-image-description=\"&lt;p&gt;An Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;An Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg\" class=\"wp-image-3108\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg?resize=550%2C486&#038;ssl=1\" alt=\"An Example\" width=\"550\" height=\"486\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg?resize=300%2C265&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg?resize=768%2C679&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1.jpg?w=948&amp;ssl=1 948w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3108\" class=\"wp-caption-text\"><strong>An Example<\/strong><\/figcaption><\/figure>\n<p>In the above example, function <em><strong>\u201cfoo()\u201d<\/strong><\/em> takes an integer and returns either an integer or an error code. If the supplied argument is between 10 and 100 (both included), then it returns a computed value, else it returns an error code of type <em><strong>\u201cErrCode\u201d<\/strong><\/em>.<\/p>\n<p>Let us see how this function is called inside <em><strong>\u201cmain()\u201d<\/strong><\/em>. The first call (line <em><strong>25<\/strong><\/em>) passes <em><strong>\u201c-100\u201d<\/strong><\/em>, which is outside the acceptable range. In this case, obviously, we expect the function to return an error code. The <em><strong>\u201cresult\u201d<\/strong><\/em> variable will be <em><strong>\u201ctrue\u201d<\/strong><\/em> if the function returned a computed value, false if an error code was returned. This is checked in lines <em><strong>26 &#8211; 29<\/strong><\/em> and corresponding value printed. Notice the use of <em><strong>\u201cvalue()\u201d<\/strong><\/em> and <em><strong>\u201cerror()\u201d<\/strong><\/em> member function calls to access the computed value and the error code respectively. This is convenient.<\/p>\n<p>Next, Line <em><strong>32<\/strong><\/em> passes a valid argument to the <em><strong>\u201cfoo()\u201d<\/strong><\/em> function. In this case, the <em><strong>\u201cresult\u201d<\/strong><\/em> variable will be <em><strong>\u201ctrue\u201d<\/strong><\/em> and hence line <em><strong>34<\/strong><\/em> will be executed.<\/p>\n<figure id=\"attachment_3109\" aria-describedby=\"caption-attachment-3109\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3109\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/output1-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output1.jpg\" data-orig-size=\"556,127\" 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;1683813151&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\/05\/output1.jpg\" class=\"wp-image-3109\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output1.jpg?resize=300%2C69&#038;ssl=1\" alt=\"Program Output\" width=\"300\" height=\"69\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output1.jpg?resize=300%2C69&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output1.jpg?w=556&amp;ssl=1 556w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3109\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>As in the case of <em><strong>std::optional&lt;&gt;<\/strong><\/em>, there is a <em><strong>\u201cvalue_or()\u201d<\/strong><\/em> member function that returns either the normal value, or in the case of an error return, the user-supplied value. This is shown in line <em><strong>39<\/strong><\/em>.<\/p>\n<figure id=\"attachment_3110\" aria-describedby=\"caption-attachment-3110\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3110\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/source2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg\" data-orig-size=\"917,588\" 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;1683813224&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=\"Use of &amp;#8220;value_or&amp;#8221;\" data-image-description=\"&lt;p&gt;Use of &amp;#8220;value_or&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Use of &amp;#8220;value_or&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg\" class=\"wp-image-3110\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?resize=500%2C321&#038;ssl=1\" alt=\"Use of &quot;value_or&quot;\" width=\"500\" height=\"321\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?resize=300%2C192&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?resize=768%2C492&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?resize=140%2C90&amp;ssl=1 140w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source2.jpg?w=917&amp;ssl=1 917w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3110\" class=\"wp-caption-text\"><strong>Use of &#8220;value_or&#8221;<\/strong><\/figcaption><\/figure>\n<p>In the case, because the supplied argument is outside the permitted range, an error code will be returned. Hence the <em><strong>\u201cvalue_or()\u201d<\/strong><\/em> function will return <em><strong>\u201c99999\u201d<\/strong><\/em> instead of the error code. This is convenient when we cascade function calls.<\/p>\n<figure id=\"attachment_3112\" aria-describedby=\"caption-attachment-3112\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3112\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/output2-14\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg\" data-orig-size=\"553,156\" 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;1683813268&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 from &amp;#8220;value_or()&amp;#8221;\" data-image-description=\"&lt;p&gt;Output from &amp;#8220;value_or()&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output from &amp;#8220;value_or()&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg\" class=\"size-medium wp-image-3112\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg?resize=300%2C85&#038;ssl=1\" alt=\"Output from &quot;value_or()&quot;\" width=\"300\" height=\"85\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg?resize=300%2C85&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output2.jpg?w=553&amp;ssl=1 553w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3112\" class=\"wp-caption-text\">Output from &#8220;value_or()&#8221;<\/figcaption><\/figure>\n<p>We have seen how the member function <em><strong>\u201cvalue()\u201d<\/strong><\/em> allows us to retrieve the normal return value from the function. What happens if we call this function when the function returns an error code? This is a fatal error and hence the program will be aborted. See line <em><strong>44<\/strong><\/em> below:<\/p>\n<figure id=\"attachment_3113\" aria-describedby=\"caption-attachment-3113\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3113\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/source3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg\" data-orig-size=\"939,684\" 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;1683813347&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=\"Incorrect Use of &amp;#8220;value()&amp;#8221;\" data-image-description=\"&lt;p&gt;Incorrect Use of &amp;#8220;value()&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Incorrect Use of &amp;#8220;value()&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg\" class=\"wp-image-3113\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg?resize=500%2C364&#038;ssl=1\" alt=\"Incorrect Use of &quot;value()&quot;\" width=\"500\" height=\"364\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg?resize=300%2C219&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg?resize=768%2C559&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source3.jpg?w=939&amp;ssl=1 939w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3113\" class=\"wp-caption-text\"><strong>Incorrect Use of &#8220;value()&#8221;<\/strong><\/figcaption><\/figure>\n<p>Here is what happens when we run the program:<\/p>\n<figure id=\"attachment_3114\" aria-describedby=\"caption-attachment-3114\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3114\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/output3-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg\" data-orig-size=\"610,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;1683813389&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=\"Abnormal Program Termination\" data-image-description=\"&lt;p&gt;Abnormal Program Termination&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Abnormal Program Termination&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg\" class=\"size-medium wp-image-3114\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg?resize=300%2C249&#038;ssl=1\" alt=\"Abnormal Program Termination\" width=\"300\" height=\"249\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg?resize=300%2C249&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output3.jpg?w=610&amp;ssl=1 610w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3114\" class=\"wp-caption-text\"><strong>Abnormal Program Termination<\/strong><\/figcaption><\/figure>\n<p>Finally, can <em><strong>std::expected&lt;&gt;<\/strong><\/em> be used with <em><strong>\u201cvoid\u201d<\/strong><\/em> returning functions? Yes, that is possible. See this code snippet:<\/p>\n<figure id=\"attachment_3115\" aria-describedby=\"caption-attachment-3115\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3115\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/source4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg\" data-orig-size=\"1066,404\" 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;1683879539&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=\"Using with &amp;#8220;void&amp;#8221;-returning Function\" data-image-description=\"&lt;p&gt;Using with &amp;#8220;void&amp;#8221;-returning Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using with &amp;#8220;void&amp;#8221;-returning Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4-1024x388.jpg\" class=\"wp-image-3115\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?resize=500%2C189&#038;ssl=1\" alt=\"Using with &quot;void&quot;-returning Function\" width=\"500\" height=\"189\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?resize=300%2C114&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?resize=1024%2C388&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?resize=768%2C291&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source4.jpg?w=1066&amp;ssl=1 1066w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3115\" class=\"wp-caption-text\"><strong>Using with &#8220;void&#8221;-returning Function<\/strong><\/figcaption><\/figure>\n<p>Of course, in this case, there is no <em><strong>\u201cnormal\u201d<\/strong><\/em> return value, but we can access the error code, if any, as usual.<\/p>\n<figure id=\"attachment_3116\" aria-describedby=\"caption-attachment-3116\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3116\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/output4-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output4.jpg\" data-orig-size=\"557,112\" 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;1683879622&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\/05\/output4.jpg\" class=\"size-medium wp-image-3116\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output4.jpg?resize=300%2C60&#038;ssl=1\" alt=\"Program Output\" width=\"300\" height=\"60\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output4.jpg?resize=300%2C60&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/output4.jpg?w=557&amp;ssl=1 557w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3116\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>It is a matter of debate\/style whether such functions should return the error code via the normal return type or should use <em><strong>std::expected&lt;&gt;<\/strong><\/em>. I personally prefer the former.<\/p>\n<p>I should stress at this point that <em><strong>std::expected&lt;&gt;<\/strong><\/em> does not replace the correct use of exceptions in the code. When to <em><strong>\u201creturn\u201d<\/strong><\/em> an error code or <em><strong>\u201cthrow\u201d<\/strong><\/em> an exception is an important design decision and must be taken seriously.<\/p>\n<p>I tried the above example in <em><strong>Visual Studio Professional (64 bit) Ver 17.5.5<\/strong><\/em> on <em><strong>Windows 10<\/strong><\/em>. You can download the source from <a href=\"https:\/\/www.rangakrish.com\/downloads\/Expected.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>I had written about std::optional&lt;&gt; in an earlier article. C++23 introduces std::expected&lt;&gt; as an interesting extension to std::optional&lt;&gt;. Whereas std::optional&lt;&gt; contains a value or none at all, std::expected&lt;&gt; contains a value or an error code associated with it. This gives better control to the caller in terms of handling the outcome of the function call. [&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":[361,362],"class_list":["post-3106","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c23","tag-stdexpected"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-O6","jetpack-related-posts":[{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":3106,"position":0},"title":"C++17: std::optional","author":"admin","date":"November 21, 2017","format":false,"excerpt":"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\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-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":3106,"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":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":3106,"position":2},"title":"std::array vs. std::vector","author":"admin","date":"August 9, 2022","format":false,"excerpt":"One of the common questions when starting to study the container abstractions in C++20 is \"Should I use std::array\u00a0or std::vector?\". The correct answer is \"use what is best suited for the current situation\". That is not good enough, obviously. In this short article, I will try to answer this question\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::array Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":3106,"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":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":3106,"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":2975,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","url_meta":{"origin":3106,"position":5},"title":"Std::tie","author":"admin","date":"December 25, 2022","format":false,"excerpt":"std::tuple is a widely used abstraction in C++ and has been around since C++11. It is a generalization of std::pair. std::tie is convenient when we want to create a tuple of lvalue references to existing variables. It is a function template commonly used to unpack a tuple into individual objects.\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::tie Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3106","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=3106"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3106\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}