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
?
Asked
Active
Viewed 1.8k times
80
-
"Apparently, consteval is going to be a keyword in C++20" - provide a link to support this assertion. – Nov 17 '18 at 01:17
-
12@NeilButterworth The cppreference page I already linked to claims it to be. – KevinZ Nov 17 '18 at 01:19
-
`decltype(std::declval
)`'ish but .. fun stuff. – Ted Lyngmo Nov 17 '18 at 01:20 -
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. – Nov 17 '18 at 01:21
-
18@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.](https://herbsutter.com/2018/11/13/trip-report-fall-iso-c-standards-meeting-san-diego/) – Nicol Bolas Nov 17 '18 at 01:36
-
1[cppreference now provides some info](https://en.cppreference.com/w/cpp/language/consteval) – Zereges Sep 08 '19 at 19:44
1 Answers
64
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).

Rakete1111
- 47,013
- 16
- 123
- 162

T.C.
- 133,968
- 17
- 288
- 421
-
-
4
-
10It 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. – Davis Herring Dec 24 '18 at 21:08
-
1@geza 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: * https://stackoverflow.com/questions/13299394/is-is-constexpr-possible-in-c11/13305072#13305072 * https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/ – Andry Jan 28 '19 at 15:07
-
@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. – geza Jan 28 '19 at 15:35
-
@geza If you don't beleave then you can always test the code generation in https://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. – Andry Jan 28 '19 at 15:42
-
2@Andry: it's not just about compile-timedness. It's about that the result is wrong. Check out: https://godbolt.org/z/12aZCH. The result depends on optimization level. – geza Jan 28 '19 at 15:47
-
@geza may be particularly for `gcc` you have to use `noexcept` instead of `__builtin_constant_p` because it works in `clang` as expected: https://godbolt.org/z/e4V41F (just switch to clang and see for yourself) – Andry Jan 28 '19 at 16:19
-
1@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. – geza Jan 28 '19 at 17:13
-
@geza accepted answers on the stackoverflow is not always the best, especially after the time have pass – Andry Jan 28 '19 at 18:18
-
A few useful concrete examples can be found at https://awfulcode.io/2019/01/26/immediate-functions-in-c20/ – Bart Jun 01 '20 at 15:22
-
2
-
4That'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. – jorgbrown Sep 24 '20 at 05:13