{"id":3216,"date":"2023-09-24T08:42:12","date_gmt":"2023-09-24T03:12:12","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3216"},"modified":"2023-09-24T16:06:38","modified_gmt":"2023-09-24T10:36:38","slug":"c23-if-consteval","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/24\/c23-if-consteval\/","title":{"rendered":"C++23: if consteval"},"content":{"rendered":"<p><em><strong>\u201cIf consteval\u201d<\/strong><\/em> is useful in the context of calling a <em>consteval<\/em> function from within a <em><strong>constexpr<\/strong><\/em> function.<\/p>\n<p>A <em><strong>consteval<\/strong><\/em> function can only be invoked in a constant expression and hence is evaluated at compile-time. On the other hand, a <em><strong>constexpr<\/strong><\/em> function may be invoked in a constant expression or non-constant expression. If invoked in a constant expression, it will be evaluated at compile-time, else it is executed at run-time. Both are implicitly inline.<\/p>\n<p>Because of the <em><strong>dual<\/strong><\/em> nature of a <em><strong>constexpr<\/strong><\/em> function (i.e., evaluated at compile-time or at run-time), it can be tricky to invoke a <em><strong>consteval<\/strong><\/em> function inside a <em><strong>constexpr<\/strong><\/em> function. Here is where the newly introduced <em><strong>C++23<\/strong> <\/em>construct <em><strong>\u201cif consteval\u201d<\/strong><\/em> offers help.<\/p>\n<p>The following example shows how it can be used:<\/p>\n<figure id=\"attachment_3217\" aria-describedby=\"caption-attachment-3217\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3217\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/24\/c23-if-consteval\/source\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png\" data-orig-size=\"586,863\" 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=\"Using &amp;#8220;if consteval&amp;#8221;\" data-image-description=\"&lt;p&gt;Using &amp;#8220;if consteval&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using &amp;#8220;if consteval&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png\" class=\"wp-image-3217\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png?resize=500%2C736&#038;ssl=1\" alt=\"Using &quot;if consteval&quot;\" width=\"500\" height=\"736\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png?resize=204%2C300&amp;ssl=1 204w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/source.png?w=586&amp;ssl=1 586w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-3217\" class=\"wp-caption-text\"><strong>Using &#8220;if consteval&#8221;<\/strong><\/figcaption><\/figure>\n<p>Let us try to understand the key parts of the code. <em><strong>bar()<\/strong><\/em> is a <em><strong>consteval<\/strong><\/em> function. <em><strong>foo()<\/strong><\/em> is a <em><strong>constexpr<\/strong><\/em> function. Inside this function, we wish to call <em><strong>bar()<\/strong><\/em> when it is <em><strong>safe <\/strong><\/em>to call it, otherwise we want some other code to be executed. <em><strong>Safe<\/strong><\/em> here means that <em><strong>foo()<\/strong><\/em> is executing in the context of a constant expression. This is where <em><strong>\u201cif consteval\u201d<\/strong><\/em> comes in handy. We can now wrap the call to <em><strong>bar()<\/strong><\/em> in a <em><strong>\u201cif consteval\u201d<\/strong><\/em> block.<\/p>\n<p>Line 29 uses <em><strong>static_assert<\/strong><\/em>, which is a compile-time assertion, to check whether <em><strong>foo()<\/strong><\/em> works correctly. As you can infer, <em><strong>foo()<\/strong><\/em> is now evaluated at compile-time since <em><strong>\u201c5\u201d<\/strong><\/em> is a constant expression, and this enables the call to <em><strong>bar()<\/strong> <\/em>wrapped inside the <em><strong>\u201cif consteval\u201d<\/strong><\/em> block.<\/p>\n<p>Lines 30 and 31 define two <em><strong>constexpr<\/strong><\/em> variables using <em><strong>foo()<\/strong><\/em> and <em><strong>bar()<\/strong><\/em> respectively. The call to <em><strong>bar()<\/strong> <\/em>is once again in the context of a constant expression and hence is valid.<\/p>\n<p>In contrast, line 35 invoke<em><strong>s foo()<\/strong> <\/em>in a non-constant expression and hence, inside <em><strong>foo()<\/strong><\/em>, the call to <em><strong>bar()<\/strong><\/em> is bypassed.<\/p>\n<p>Line 39 (commented out) calls <em><strong>bar()<\/strong> <\/em>with a non-constant expression and hence is invalid.<\/p>\n<p>When the program is executed, it generates the following output:<\/p>\n<figure id=\"attachment_3222\" aria-describedby=\"caption-attachment-3222\" style=\"width: 350px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/result-2.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3222\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/09\/24\/c23-if-consteval\/result-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/result-2.png\" data-orig-size=\"418,67\" 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=\"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\/09\/result-2.png\" class=\"wp-image-3222\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/result-2.png?resize=350%2C56&#038;ssl=1\" alt=\"Program Output\" width=\"350\" height=\"56\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/result-2.png?resize=300%2C48&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/09\/result-2.png?w=418&amp;ssl=1 418w\" sizes=\"(max-width: 350px) 100vw, 350px\" \/><\/a><figcaption id=\"caption-attachment-3222\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The output matches the expected program behaviour.<\/p>\n<p>You can view the program in <a href=\"https:\/\/compiler-explorer.com\/z\/dMeoWhTPe\" target=\"_blank\" rel=\"noopener\"><em><strong>Compiler Explorer.<\/strong><\/em><\/a><\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\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 expression. If invoked in a [&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,370,369,368],"class_list":["post-3216","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c23","tag-consteval","tag-constexpr","tag-if-consteval"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-PS","jetpack-related-posts":[{"id":739,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/","url_meta":{"origin":3216,"position":0},"title":"C++17: constexpr if","author":"admin","date":"December 17, 2017","format":false,"excerpt":"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\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-1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3230,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/10\/15\/c20-constinit-specifier\/","url_meta":{"origin":3216,"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":3035,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/03\/05\/julia-defining-functions-dynamically\/","url_meta":{"origin":3216,"position":2},"title":"Julia: Defining Functions Dynamically","author":"admin","date":"March 5, 2023","format":false,"excerpt":"One of the widely discussed features of Julia is its support for metaprogramming. This feature makes it possible to generate and inspect the code at runtime. In essence, metaprogramming blurs the distinction between code and data. When used carefully, it can contribute to good code. Of course, Julia is not\u2026","rel":"","context":"In &quot;Julia&quot;","block_context":{"text":"Julia","link":"https:\/\/www.rangakrish.com\/index.php\/category\/julia\/"},"img":{"alt_text":"Defining A Function Dynamically","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/03\/Example1-300x195.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":3216,"position":3},"title":"C++17: Initialization in Selection Statements","author":"admin","date":"October 21, 2017","format":false,"excerpt":"C++17 enhances if and switch statements with the ability to define\u00a0 variables whose life-time is limited to the corresponding scope. This is in keeping with the general guideline that variables should have a tight scope, i.e.,\u00a0 should be defined as close to the point of use as possible and should\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Sample Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/classdefn.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2722,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/03\/06\/lazy-parameter-evaluation-in-d-language\/","url_meta":{"origin":3216,"position":4},"title":"Lazy Parameter Evaluation in D Language","author":"admin","date":"March 6, 2022","format":false,"excerpt":"Languages such as C\/C++ and Java follow an\u00a0\"eager\" argument evaluation approach, where function arguments are evaluated before the function is entered. Of course, there are idioms and tricks that one can use to force \"lazy\" evaluation. The primary benefit of \"lazy\" parameter evaluation is that the parameter is evaluated only\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Example Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/03\/eagercode-237x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":3216,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3216","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=3216"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3216\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}