Reference:
https://andreasfertig.blog/2021/07/cpp20-a-neat-trick-with-consteval/
What consteval does:
As the name of the keyword tries to imply, it forces a constant evaluation.
In the standard, a function that is marked as consteval is called an immediate function.
The keyword can be applied only to functions/function template.
Immediate here means that the function is evaluated at the front-end, yielding only a value, which the back-end uses.
Such a function never goes into your binary.
A consteval-function must be evaluated at compile-time or compilation fails.
With that, a consteval-function is a stronger version of constexpr-functions.
template <auto value>
inline constexpr auto as_constant = value;
constexpr int Calc(int x) { return 4 * x; }
int main() {
auto res = as_constant<Calc(2)>;
++res;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.