What is consteval?
Asked Answered
B

1

86

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?

Beginner answered 17/11, 2018 at 1:13 Comment(6)
"Apparently, consteval is going to be a keyword in C++20" - provide a link to support this assertion.Beauvais
@NeilButterworth The cppreference page I already linked to claims it to be.Beginner
decltype(std::declval<f()>)'ish but .. fun stuff.Electromotive
The C++20 Standard has not yet been published, so cppreference (or anyone else) can't say what will appear in it and what the semantics of it will be - if it exists at all.Beauvais
@NeilButterworth: By that reasoning, nobody can even call it "C++20" or say that there will even be a next version of C++. It is not unreasonable to ask about upcoming features that have been approved in accord with WG21 procedures at the various meetings. Like the most recent one.Contagium
cppreference now provides some infoNosography
W
66

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).

Wendeline answered 17/11, 2018 at 1:28 Comment(13)
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 passArmenta
A few useful concrete examples can be found at awfulcode.io/2019/01/26/immediate-functions-in-c20Forewarn
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.