{"id":6302,"date":"2022-02-07T06:59:47","date_gmt":"2022-02-07T06:59:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/constespr-if\/"},"modified":"2023-06-26T09:13:39","modified_gmt":"2023-06-26T09:13:39","slug":"constespr-if","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/constespr-if\/","title":{"rendered":"constexpr if"},"content":{"rendered":"<p>In today&#8217;s post, I want to introduce an exciting C++17 feature: <code>constexpr if. constexpr if<\/code> enables it to compile source code conditionally and can be used for nice tricks at compile time.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6301\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/02\/constexprIf.png\" alt=\"constexprIf\" width=\"650\" height=\"413\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/02\/constexprIf.png 898w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/02\/constexprIf-300x191.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/02\/constexprIf-768x488.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>Introducing <code>constexpr if<\/code> is straightforward.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> getValue(T t) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (std<span style=\"color: #555555;\">::<\/span>is_pointer_v<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>)     <em><span style=\"color: #0099ff;\"> \/\/ (1)<\/span><\/em>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span>t; <span style=\"color: #0099ff; font-style: italic;\">\/\/ deduces return type to int for T = int*<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">else                                    <\/span><span style=\"color: #006699;\"> <\/span><span style=\"color: #006699;\"><em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em><\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> t;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ deduces return type to int for T = int<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p><!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<p>The code snippet shows one exciting fact about<code> constexpr if<\/code>: Although it is called <code>constexpr if<\/code>, it is used as <code>if constexpr<\/code>: <code>if constexpr (std::is_pointer_v&lt;T&gt;<\/code>).<\/p>\n<p>If<code> T<\/code> is a pointer, the if branch in line (1) will be compiled. If not, the else branch in line (2). Two points are important to mention. The function<code> getValue<\/code> has two different return types, and both branches of the <code>if<\/code> statement have to be valid.<\/p>\n<p>The expression in constexpr if has to be a compile-time predicate. A compile time predicate is a function that returns a boolean and runs at compile time. I used in the code snippet a function from the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/header\/type_traits\">type-traits library<\/a>. Alternatively, in C++20, you can use a concept. Here is the equivalent example using the concept<code> std::integral:<\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> get_value(T t) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (std<span style=\"color: #555555;\">::<\/span>integral<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>)          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span>t; <span style=\"color: #0099ff; font-style: italic;\">\/\/ deduces return type to int for T = int*<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>                                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> t;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ deduces return type to int for T = int<\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I see, the two code snippets are not so impressive. Let me continue with template metaprogramming.<\/p>\n<p>Thanks to<code> constexpr if<\/code>, template metaprogramming is often easier to write and read.<\/p>\n<\/p>\n<h2>Template Metaprogramming with <code>constexpr if<\/code><\/h2>\n<p>Metaprogramming is programming on programs. C++ applies metaprogramming at compile time. It started in C++98 with template metaprogramming, was formalized in C++11 with the type-traits library, and since C++11 has steadily improved.<\/p>\n<p>Here is the &#8220;Hello World&#8221; of template metaprogramming: calculating the factorial of a number:<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ factorial.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>                                                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Factorial{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> N <span style=\"color: #555555;\">*<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;::<\/span>value;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                                                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n';\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Factorial&lt;5&gt;::value: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n'<span style=\"color: #555555;\"><\/span>;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Factorial&lt;10&gt;::value: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n'<span style=\"color: #555555;\"><\/span>;  <em><span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> '\\n'<span style=\"color: #555555;\"><\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The call<code> factorial&lt;5&gt;::value<\/code> (line 1) causes the instantiation of the primary or general template (line 2). During this instantiation, <code>Factorial&lt;4&gt;::value<\/code> will be instantiated. This recursion will end if the fully specialized class template<code> Factorial&lt;1&gt; <\/code>kicks in (line 3).<\/p>\n<p>If you want to know more about template metaprogramming, read my previous posts:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-a-introduction\">Template Metaprogramming &#8211; How it All Started<\/a><\/li>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-how-it-works\">Template Metaprogramming &#8211; How it Works<\/a><\/li>\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/template-metaprogramming-hybrid-programming\">Template Metaprogramming &#8211; Hybrid Programming<\/a><\/li>\n<\/ol>\n<p>Let me rewrite the program using <code>constexpr i<\/code>f:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ factorialConstexprIf.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Factorial{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> N <span style=\"color: #555555;\">*<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;::<\/span>value;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>                                              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> factorial() {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (N <span style=\"color: #555555;\">&gt;=<\/span> <span style=\"color: #ff6600;\">2<\/span>) \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> N <span style=\"color: #555555;\">*<\/span> factorial<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>();\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span> \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> N;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n    \r\n    static_assert(Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">==<\/span> factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">5<\/span><span style=\"color: #555555;\">&gt;<\/span>());     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)             <\/span>\r\n    static_assert(Factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">==<\/span> factorial<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">&gt;<\/span>());   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The primary template of <code>Factorial<\/code> (line 1) becomes the if condition in the <code>constexpr<\/code> function <code>factorial<\/code> (line 3), and the full specialization of <code>Factorial<\/code> for 1 (line 2) becomes the else case in the <code>constexpr<\/code> function factorial (line 3). Of course, the class template <code>Factorial<\/code> and the <code>constexpr<\/code> function <code>factorial<\/code> return the same result and are executed at compile time (line 4). To make it short, I prefer the constexpr function using<code> constexpr if<\/code> because it reads almost such as a usual function.<\/p>\n<p>Let&#8217;s do it once more. Here is the infamous Fibonacci function-based template metaprogramming (<code>Fibonacci<\/code>) and <code>constexpr if (fibonacci).<br \/><\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ fibonacciConstexprIf.cpp<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> fibonacci()\r\n{\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> constexpr (N<span style=\"color: #555555;\">&gt;=<\/span><span style=\"color: #ff6600;\">2<\/span>)\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> fibonacci<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>() <span style=\"color: #555555;\">+<\/span> fibonacci<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;<\/span>();\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> N;\r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span> N<span style=\"color: #555555;\">&gt;<\/span>                                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)            <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Fibonacci{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> Fibonacci<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;::<\/span>value <span style=\"color: #555555;\">+<\/span> Fibonacci<span style=\"color: #555555;\">&lt;<\/span>N<span style=\"color: #555555;\">-<\/span><span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">&gt;::<\/span>value;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)                <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Fibonacci<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">1<\/span><span style=\"color: #555555;\">&gt;<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">1<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;&gt;<\/span>                                                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)                <\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">struct<\/span> Fibonacci<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">0<\/span><span style=\"color: #555555;\">&gt;<\/span>{\r\n    <span style=\"color: #006699; font-weight: bold;\">static<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #006699; font-weight: bold;\">const<\/span> value <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    static_assert(fibonacci<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">7<\/span><span style=\"color: #555555;\">&gt;<\/span>() <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">13<\/span>);\r\n    static_assert(fibonacci<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">7<\/span><span style=\"color: #555555;\">&gt;<\/span>() <span style=\"color: #555555;\">==<\/span> Fibonacci<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #ff6600;\">7<\/span><span style=\"color: #555555;\">&gt;::<\/span>value);\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The <code>constexpr<\/code> function <code>fibonacci<\/code> is straightforward to read. The entire functionality is in one function body. In contrast, the template metaprogram <code>Fibonacci<\/code> requires three classes. The primary template (line 1) and the two full specializations for the values 1 and 0 (lines 2 and 3).<\/p>\n<h2>More Information about my Mentoring Program, &#8220;Fundamentals for C++ Professionals&#8221;<\/h2>\n<p>I created the platform for my new mentoring on <a href=\"https:\/\/www.modernescpp.org\/\">https:\/\/www.modernescpp.org\/<\/a>. You can skip through each of the 28 lessons. I also presented the 6th lesson about move semantics and perfect forwarding in the post &#8216;<a href=\"https:\/\/www.modernescpp.org\/moredetails\/\">More Information about my Mentoring Program &#8220;Fundamentals for C++ Professionals&#8221;&#8216;<\/a>. Here are the next steps before I start the mentoring program.<\/p>\n<ul>\n<li><strong>Beginning of March<\/strong>: online information session about my mentoring program, where you can also ask your questions<\/li>\n<li><strong>Middle of March:<\/strong> my mentoring program opens for registration<\/li>\n<li><strong>April:<\/strong> registration for the mentoring program closes, and the mentoring program starts<\/li>\n<\/ul>\n<p>If you want to stay informed, write an e-mail to <span id=\"cloaka8aba3920e2314502dad0e30d36d79d0\"><a href=\"mailto:info@ModernesCpp.de\">info@ModernesCpp.de<\/a><\/span> with the subject &#8220;Mentoring&#8221;. Write me also an e-mail if you need more information.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>Templates are a powerful tool and, therefore, provide new design choices. In my next post, I will write about static and dynamic polymorphism.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s post, I want to introduce an exciting C++17 feature: constexpr if. constexpr if enables it to compile source code conditionally and can be used for nice tricks at compile time.<\/p>\n","protected":false},"author":21,"featured_media":6301,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[376],"tags":[421],"class_list":["post-6302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-templates","tag-constexpr-if"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6302"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6302\/revisions"}],"predecessor-version":[{"id":6682,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6302\/revisions\/6682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6301"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}