consteval is a modifier introduced in C++20, which informs the compiler that the function is immediate - it can only be calculated at compile time. It is similar to modifier constexpr but it enforces compile time evaluation, not only allows it.
Questions tagged [consteval]
79 questions
8
votes
1 answer
Nested call of consteval functions with a reference argument
The following program
template
consteval auto foo(const T&) {
return 0;
}
template
consteval auto bar(const T& t) {
auto n = foo(t);
return n;
}
int main() {
static_assert(foo("abc") == 0);
static_assert(bar("abc")…

Fedor
- 17,146
- 13
- 40
- 131
8
votes
2 answers
clang says call to void consteval function is not a constant expression
clang(trunk) gives an error for the following code:
consteval void f() {}
int main()
{
f(); // error: call to consteval function 'f' is not a constant expression
// note: subobject of type 'void' is not initialized
}
while…

cigien
- 57,834
- 11
- 73
- 112
7
votes
2 answers
Is gcc wrong to allow the initialization of a const array member with another array reference?
While (re)implementing a simple constexpr map, I wrote this
(godbolt):
template
class flat_map
{
private:
struct pair
{
key_type key;
value_type value;
};
const pair…

MatG
- 574
- 2
- 7
- 19
6
votes
1 answer
decltype(fun()) for consteval methods
Consider the following code. I can compile it with GCC 10.2.0 and Clang 11.0.0 (as expected):
#include
template
struct T {
static constexpr auto fun() noexcept { return 0; }
using type = std::remove_cvref_t;…

Koosha
- 1,492
- 7
- 19
6
votes
1 answer
What are the advantages of using consteval instead of constexpr function?
I know the difference in requirements, I am mostly interested in what benefits from code quality it brings.
Few things I can think of:
reader can just read the function signature and know that function is evaluated at compile time
compiler may emit…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
6
votes
1 answer
How does consteval influence evaluation of default arguments?
In the code below, if here() stops being consteval (either full RT, or constexpr), then line() is the line of invocation of f() inside main(). But with consteval it's the definition of f(). Where does this discrepancy come from?
#include…

Marcin Zdun
- 142
- 1
- 8
5
votes
3 answers
consteval lambda with ignored parameter doesn't compile
I have a lambda that ignores its int parameter and always returns a constant.
If I mark it consteval, compilation fails because.
The compiler complains about invoking the consteval lambda with a non-const parameter.
But what does the parameter has…

Benedetto
- 711
- 6
- 17
5
votes
1 answer
Consteval constructor and member function calls in constexpr functions
struct A {
int i;
consteval A() { i = 2; };
consteval void f() { i = 3; }
};
constexpr bool g() {
A a;
a.f();
return true;
}
int main() {
static_assert(g());
}
https://godbolt.org/z/hafcab7Ga
The program is…

user17732522
- 53,019
- 2
- 56
- 105
5
votes
1 answer
In which context default argument of immediate function is substituted in C++20 (on source location example)?
In C++20 a new feature was added to get the source location information: https://en.cppreference.com/w/cpp/utility/source_location
Here is a slightly modified example from that page, where an addition immediate function loc is used to get the source…

Fedor
- 17,146
- 13
- 40
- 131
5
votes
3 answers
How to fail a consteval function?
I have the following function:
template
consteval size_t indexOf(SomeEnum someEnum,
const std::array &arr) {
for (size_t i = 0; i < TSize; ++i) {
if (arr[i] == someEnum) {
return…

nihohit
- 498
- 3
- 23
5
votes
1 answer
Compile-time debugging in C++
Under the maxim constexpr everything and with the introduction of consteval in C++20, more and more code is evaluated at compile time.
This leads to the obvious question: How do we debug this?
The only hints, currently, are compiler errors. But what…

non-user38741
- 671
- 5
- 19
4
votes
2 answers
Should user defined literals always be consteval in C++20?
If I'm not mistaken, the arguments for a user defined literal are always known at compile time. In C++20 you can force functions to execute at compile time using consteval so that throw generates a compile time error.
#include
consteval…

Benjamin Buch
- 4,752
- 7
- 28
- 51
4
votes
0 answers
Is it possible to create a dynamic list by code at compile time and have it act as a const array at runtime?
I'm currently doing some work and I would like to abstract as much code as possible into a simple api. Essentially I want to write something like this:
int main()
{
Server server;
server.get<"/hello">([] { std::cout << "hello" << std::endl;…

Thomas
- 41
- 1
4
votes
1 answer
Immediate function as default function argument initializer in Clang++
If one uses an immediate function (declared with consteval) for default initialization of a global function argument like here
consteval int foo() { return 0; }
int bar(int a = foo()) { return a; }
int main() { return bar(); }
then Clang compiler…

Fedor
- 17,146
- 13
- 40
- 131
4
votes
1 answer
Why is there such a massive difference in compile time between consteval/constexpr and template metafunctions?
I was curious how far I could push gcc as far as compile-time evaluation is concerned, so I made it compute the Ackermann function, specifically with input values of 4 and 1 (anything higher than that is impractical):
consteval unsigned int…

Bridge
- 105
- 1
- 8