{"id":739,"date":"2017-12-17T08:52:50","date_gmt":"2017-12-17T08:52:50","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=739"},"modified":"2017-12-17T09:00:17","modified_gmt":"2017-12-17T09:00:17","slug":"c17-constexpr-if","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/","title":{"rendered":"C++17: constexpr if"},"content":{"rendered":"<p><b><i>constexpr if <\/i><\/b>is another nice little feature in C++17, which significantly simplifies the way we code, especially in the context of templates. In this article, I will briefly talk about this feature and look at some examples.<\/p>\n<p><b><i>constexpr if <\/i><\/b>is a variant of the standard <b><i>if<\/i><\/b> statement we have had all along. The primary difference\u00a0 is that the condition part in <b><i>constexpr if <\/i><\/b>must be a compile-time evaluable expression unlike the standard <b><i>if<\/i><\/b>, where the condition is evaluated at run-time. Because of this special requirement, the compiler guarantees that the code it generates for the <b><i>constexpr if <\/i><\/b>will just be the sub-part of the <b><i>if<\/i><\/b> statement that is reachable. Take a look at the following example.<\/p>\n<figure id=\"attachment_740\" aria-describedby=\"caption-attachment-740\" style=\"width: 380px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"740\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example1-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png\" data-orig-size=\"380,246\" 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\/12\/Example1-1.png\" class=\"size-full wp-image-740\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?resize=380%2C246\" alt=\"Example1\" width=\"380\" height=\"246\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?w=380&amp;ssl=1 380w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?resize=300%2C194&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?resize=140%2C90&amp;ssl=1 140w\" sizes=\"(max-width: 380px) 100vw, 380px\" \/><\/a><figcaption id=\"caption-attachment-740\" class=\"wp-caption-text\"><strong>Example1<\/strong><\/figcaption><\/figure>\n<p>As you can see, the function <b><i>checkCondition()<\/i><\/b> in the above example is a <b><i>constexpr<\/i><\/b> function, in that its return value is known at compile-time. (In this article, we are not going into the details of <b><i>constexpr <\/i><\/b>in general. If time permits, I will cover that in a separate future article.)<\/p>\n<p>So, what is the code emitted by the compiler for the <b><i>constexpr if <\/i><\/b>statement? The figure below shows the assembly code generated by <b><i>Visual Studio 2017.<\/i><\/b><\/p>\n<figure id=\"attachment_741\" aria-describedby=\"caption-attachment-741\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"741\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example1b\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png\" data-orig-size=\"786,440\" 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=\"Generated Code\" data-image-description=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png\" class=\"wp-image-741\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png?resize=650%2C364\" alt=\"Generated Code\" width=\"650\" height=\"364\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png?w=786&amp;ssl=1 786w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png?resize=768%2C430&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example1b.png?resize=352%2C198&amp;ssl=1 352w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-741\" class=\"wp-caption-text\"><strong>Generated Code<\/strong><\/figcaption><\/figure>\n<p>You can see that the false part of the <b><i>if<\/i><\/b> statement alone has been considered. The reason is, the compiler figured out that the condition is false (always) and hence the true part of the <b><i>if<\/i><\/b> statement can never get a chance to execute!<\/p>\n<p>Just to compare with the standard <b><i>if <\/i><\/b>statement, take a look at the following code:<\/p>\n<figure id=\"attachment_742\" aria-describedby=\"caption-attachment-742\" style=\"width: 363px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2-1.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"742\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example2-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2-1.png\" data-orig-size=\"363,155\" 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\/12\/Example2-1.png\" class=\"size-full wp-image-742\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2-1.png?resize=363%2C155\" alt=\"Example2\" width=\"363\" height=\"155\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2-1.png?w=363&amp;ssl=1 363w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2-1.png?resize=300%2C128&amp;ssl=1 300w\" sizes=\"(max-width: 363px) 100vw, 363px\" \/><\/a><figcaption id=\"caption-attachment-742\" class=\"wp-caption-text\"><strong>Example2<\/strong><\/figcaption><\/figure>\n<p>Here is the assembly code generated for this code:<\/p>\n<figure id=\"attachment_743\" aria-describedby=\"caption-attachment-743\" style=\"width: 651px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"743\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example2b\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png\" data-orig-size=\"816,538\" 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=\"Generated Code\" data-image-description=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png\" class=\"wp-image-743\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png?resize=651%2C429\" alt=\"Generated Code\" width=\"651\" height=\"429\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png?w=816&amp;ssl=1 816w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png?resize=300%2C198&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example2b.png?resize=768%2C506&amp;ssl=1 768w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption id=\"caption-attachment-743\" class=\"wp-caption-text\"><strong>Generated Code<\/strong><\/figcaption><\/figure>\n<p>Because the logic for checking the condition is executed at run-time (as opposed to compile-time) in this case, the compiler has to emit code for both the true and false parts of the <b><i>if<\/i><\/b> statement. It cannot assume that only one of the two will always execute.<\/p>\n<p>So, compared to the regular <b><i>if <\/i><\/b>statement, <b><i>constexpr if <\/i><\/b>results in more compact code. The limitation, obviously, is that the condition must be a <em><strong>compile-time knowable<\/strong><\/em> expression. For example, the following code will not compile because the condition cannot be evaluated at compile time.<\/p>\n<figure id=\"attachment_744\" aria-describedby=\"caption-attachment-744\" style=\"width: 463px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"744\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/constexprcheck\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png\" data-orig-size=\"463,176\" 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=\"Compile-time Check Needed\" data-image-description=\"&lt;p&gt;Compile-time Check Needed&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Compile-time Check Needed&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png\" class=\"size-full wp-image-744\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png?resize=463%2C176\" alt=\"Compile-time Check Needed\" width=\"463\" height=\"176\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png?w=463&amp;ssl=1 463w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/constexprcheck.png?resize=300%2C114&amp;ssl=1 300w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/a><figcaption id=\"caption-attachment-744\" class=\"wp-caption-text\"><strong>Compile-time Check Needed<\/strong><\/figcaption><\/figure>\n<p>Here is another important thing to remember. Even though we might know that one part of the <b><i>constexpr if <\/i><\/b>statement will not be compiled, we cannot have invalid C++ statements in that part. This will be clear from the following example.<\/p>\n<figure id=\"attachment_747\" aria-describedby=\"caption-attachment-747\" style=\"width: 505px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"747\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example3-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3-1.png\" data-orig-size=\"505,180\" 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\/12\/Example3-1.png\" class=\"size-full wp-image-747\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3-1.png?resize=505%2C180\" alt=\"Example3\" width=\"505\" height=\"180\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3-1.png?w=505&amp;ssl=1 505w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3-1.png?resize=300%2C107&amp;ssl=1 300w\" sizes=\"(max-width: 505px) 100vw, 505px\" \/><\/a><figcaption id=\"caption-attachment-747\" class=\"wp-caption-text\"><strong>Example3<\/strong><\/figcaption><\/figure>\n<p>In this respect, it is different from <em><strong>#if<\/strong><\/em> or <em><strong>#ifdef.<\/strong><\/em> Check this out:<\/p>\n<figure id=\"attachment_749\" aria-describedby=\"caption-attachment-749\" style=\"width: 393px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"749\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/if\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png\" data-orig-size=\"393,77\" 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=\"Preprocessor Example\" data-image-description=\"&lt;p&gt;Preprocessor Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Preprocessor Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png\" class=\"size-full wp-image-749\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png?resize=393%2C77\" alt=\"Preprocessor Example\" width=\"393\" height=\"77\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png?w=393&amp;ssl=1 393w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/if.png?resize=300%2C59&amp;ssl=1 300w\" sizes=\"(max-width: 393px) 100vw, 393px\" \/><\/a><figcaption id=\"caption-attachment-749\" class=\"wp-caption-text\"><strong>Preprocessor Example<\/strong><\/figcaption><\/figure>\n<p><b><i>constexpr if \u00a0<\/i><\/b>is very useful in the case of templates. It can simplify things considerably. Here is an example of compile-time evaluation of a function (obviously, not suitable for serious context!):<\/p>\n<figure id=\"attachment_745\" aria-describedby=\"caption-attachment-745\" style=\"width: 439px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"745\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example4-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png\" data-orig-size=\"439,194\" 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=\"Template Example\" data-image-description=\"&lt;p&gt;Template Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Template Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png\" class=\"size-full wp-image-745\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png?resize=439%2C194\" alt=\"Template Example\" width=\"439\" height=\"194\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png?w=439&amp;ssl=1 439w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4-1.png?resize=300%2C133&amp;ssl=1 300w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/a><figcaption id=\"caption-attachment-745\" class=\"wp-caption-text\"><strong>Template Example<\/strong><\/figcaption><\/figure>\n<p>If the above function is to be re-written without <em><strong>constexpr if<\/strong><\/em> (pre C++17), then it would look like this (I have changed the function name for convenience):<\/p>\n<figure id=\"attachment_746\" aria-describedby=\"caption-attachment-746\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"746\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/example4b\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png\" data-orig-size=\"410,403\" 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=\"Template Example Pre-C++17\" data-image-description=\"&lt;p&gt;Template Example Pre-C++17&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Template Example Pre-C++17&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png\" class=\"size-full wp-image-746\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png?resize=410%2C403\" alt=\"Template Example Pre-C++17\" width=\"410\" height=\"403\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png?w=410&amp;ssl=1 410w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example4b.png?resize=300%2C295&amp;ssl=1 300w\" sizes=\"(max-width: 410px) 100vw, 410px\" \/><\/a><figcaption id=\"caption-attachment-746\" class=\"wp-caption-text\"><strong>Template Example (Pre-C++17)<\/strong><\/figcaption><\/figure>\n<p>It is clear that substantial saving in coding has been achieved by the use of <em><strong>constexpr if<\/strong><\/em>. Not only that, the logic is much clearer!<\/p>\n<p>You can find many more interesting examples if you dig into the template library. But hopefully, the above gives you an idea.<\/p>\n<p>OK, that is it for today. Have a great weekend!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>constexpr if is another nice little feature in C++17, which significantly simplifies the way we code, especially in the context of templates. In this article, I will briefly talk about this feature and look at some examples. constexpr if is a variant of the standard if statement we have had all along. The primary difference\u00a0 [&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,125],"class_list":["post-739","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c17","tag-constexpr-if"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-bV","jetpack-related-posts":[{"id":3216,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/24\/c23-if-consteval\/","url_meta":{"origin":739,"position":0},"title":"C++23: if consteval","author":"admin","date":"September 24, 2023","format":false,"excerpt":"\u201cIf consteval\u201d is useful in the context of calling a consteval function from within a constexpr function. A consteval function can only be invoked in a constant expression and hence is evaluated at compile-time. On the other hand, a constexpr function may be invoked in a constant expression or non-constant\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Using \"if consteval\"","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source-204x300.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3230,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/","url_meta":{"origin":739,"position":1},"title":"C++20: \u201cconstinit\u201d Specifier","author":"admin","date":"October 15, 2023","format":false,"excerpt":"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,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-1: Basic Types","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/10\/case1-300x178.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":739,"position":2},"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":3083,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/","url_meta":{"origin":739,"position":3},"title":"Defer Statement in V Language","author":"admin","date":"April 20, 2023","format":false,"excerpt":"The defer statement in Vlang causes code to be executed when the enclosing function returns. This feature is inspired by Golang, but is slightly more flexible since it allows any block of code to be associated with defer instead of just a function call. defer is used primarily to handle\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"The \"defer\" Statement","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1496,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/03\/10\/book-review-c-concurrency-in-action\/","url_meta":{"origin":739,"position":4},"title":"Book Review: C++ Concurrency in Action","author":"admin","date":"March 10, 2019","format":false,"excerpt":"Title: C++ Concurrency in Action Author: Anthony Williams Publisher: Manning Publications Co. Year: 2019 (Second Edition) \u00a0 The first edition of this book came out in the year 2012 and covered the concurrency features of C++ 11. Much has happened since then. The present C++17 is significantly richer in terms\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":980,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/07\/08\/book-review-the-modern-c-challenge\/","url_meta":{"origin":739,"position":5},"title":"Book Review &#8211; The Modern C++ Challenge","author":"admin","date":"July 8, 2018","format":false,"excerpt":"Title: The Modern C++ Challenge Author: Marius Bancilla Publisher: Packt Publishing Year: May 2018\u00a0 This week I have been going through a new book titled The Modern C++ Challenge written by Marius Bancilla. The book is a collection of programming problems (along with suggested solutions) targeting C++ . The title\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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/739","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=739"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/739\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}