Implicit promotion of consteval functions
In today's post, I'll explain a C++23 change when using consteval functions.
Since consteval have been added to C++20 there us a struggle where they can be used. The following code illustrates the typical pitfall:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
You have a consteval function in A which is called from a constexpr function in B. The one call-side you have in this program is with the constant value 3 from C. A perfectly constant call chain, which should compile. Yet, it doesn't. The issue is that the constexpr function can be called during run-time. In that case the parameter to the consteval function in A becomes a run-time value.
This is a situation which we can't resolve. The issue is somewhat by design. But what if Other is a function template?
1 2 3 4 5 | |
What happens in C++23 is that in this specific case the constexpr function implicitly become consteval due to the call to the consteval function Fun inside.
This especially enables more use-cases together with static reflection in C++26.
In case you struggle and want to see that implicit promotion you can now use C++ Insights which shows you the actual const-variant for a function template instantiation.
Andreas