Apparently, consteval
is going to be a keyword in C++20. The cppreference page for it is currently blank. What is it going to be and how does it relate to constexpr
?
What is consteval?
It declares immediate functions, that is, functions that must be evaluated at compile time to produce a constant. (It used to be spelled constexpr!
in a previous revision of the paper.) In contrast, constexpr
functions may be evaluated at compile time or run time, and need not produce a constant in all cases.
The adopted paper is P1073R3, which is not yet publicly available, but a previous revision is available and the introductory (motivation and high-level description) portion is about the same (except that the "Source Locations" section is deleted in R3).
Can we overload based on
consteval
-ness? –
Cresting No, you can't, just like
constexpr
. –
Wendeline It might be worth pointing out that the motivation is to have functions that rely on compiler data structures that need not be preserved in the binary. –
Worley
@Cresting You don't need to, but instead of overloading you can test an expression on compile-timeness by usage either
noexcept
operator (for msvc
or gcc
) or through the __builtin_constant_p
operator (for clang
or gcc
). Read these for details: * #13299894 * reddit.com/r/cpp/comments/7c208c/… –
Armenta @Andry: I don't think that these are 100% suited for the task.
noexcept
has its problems, as far as I remember. And __builtin_constant_p
is not usable with gcc, as its output depends on optimization level. –
Cresting @Cresting If you don't beleave then you can always test the code generation in godbolt.org with disabled optimization (
-Od
in gcc
or /Ox
in msvc
). Hint: you can pass the result of noexcept
or __builtin_constant_p
to a class template bool argument to guarantee the compile-timeness. –
Armenta @Andry: it's not just about compile-timedness. It's about that the result is wrong. Check out: godbolt.org/z/12aZCH. The result depends on optimization level. –
Cresting
@Cresting may be particularly for
gcc
you have to use noexcept
instead of __builtin_constant_p
because it works in clang
as expected: godbolt.org/z/e4V41F (just switch to clang and see for yourself) –
Armenta @Andry: about
noexcept
, check out the accepted answer at the question you've linked. I'm not sure about the current (c++17/c++20) state of this trick. –
Cresting @Cresting accepted answers on the stackoverflow is not always the best, especially after the time have pass –
Armenta
A few useful concrete examples can be found at awfulcode.io/2019/01/26/immediate-functions-in-c20 –
Forewarn
Does this mean consteval functions aren't available at runtime? –
Culpable
That's correct, @MarcusJ, consteval functions won't be available at runtime because there's no way to call them unless all their parameters are known at compile-time. And of course, if that's true, the compiler can figure out (at compile-time) what those routines return, with no need to actually generate code for them. –
Unprepared
© 2022 - 2024 — McMap. All rights reserved.
decltype(std::declval<f()>)
'ish but .. fun stuff. – Electromotive