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
1
vote
1 answer
Why a call to a consteval from object constructor is not constant expression?
I have the consteval function shown below:
template
consteval std::size_t text_id(const char(&a_text)[text_length])
{
std::size_t result{text_length};
for (const auto &c : a_text)
{
result ^= c;
…

PaperBirdMaster
- 12,806
- 9
- 48
- 94
1
vote
1 answer
non-'constexpr' function std::to_string(int) in a consteval lambda
I'm having trouble with std::to_string() in the following lambda function:
#include
#include
inline constexpr int a_global_constant { 12345 };
int main( )
{
auto calculateMsgLength = [ ] ( ) consteval -> std::size_t
…

digito_evo
- 3,216
- 2
- 14
- 42
1
vote
1 answer
What is Allowed in a consteval Function?
(See for reference What is Allowed in a constexpr Function?)
I know that constexpr functions have some restrictions upon what is allowed in them. The following is a quote lists them.
The function body may contain anything but:
an asm declaration
a…

Karen Baghdasaryan
- 2,407
- 6
- 24
1
vote
1 answer
How to serialize complex data structures as coded constexpr initializations in C++20
The goal is to serialize large data structures as constexpr initializations, so they will be part of the .text or .rodata segment and can be used for consteval, constexpr etc.
The data structures contain containers for cross-referencing/indexing…

markus
- 81
- 6
1
vote
1 answer
How come constexpr functions can not consume consteval functions while you can create constexpr objects from consteval functions?
You can have constexpr objects fron consteval
but you can not consume consteval within constexpr.
Why?
I thought consteval should have been some kind of "narrow" constexpr.
Please help me make a sense out of this design.
constexpr int…

sandthorn
- 2,770
- 1
- 15
- 59
0
votes
1 answer
Is it possible to make a compile-time evaluated function return a different type than the runtime evaluation of the same function?
Is it possible, at compile time, to determine if a call to a function (a constexpr function mainly) is compile-time evaluated and than just make another version of that function (like what a template does) with a different return type than the…

Aries Victor
- 11
- 1
0
votes
1 answer
constexpr for amortizing heavy computation
It seems that constexpr evaluation, is just a extremely slow dynamic language. Everything is allocated on the heap (even scalar types), and garbage collected.
With msvc and gcc, this program takes up all my memory (clang doesn't), and takes multiple…

Tom Huntington
- 2,260
- 10
- 20
0
votes
1 answer
Cast in consteval function fails; works outside
I'm trying to write a thin wrapper layer to interface c++ classes from python.
Python itself uses those three signatures to call a c function from py:
typedef PyObject *(*PyCFunction)(PyObject *, PyObject *);
typedef PyObject…

tkausl
- 13,686
- 2
- 33
- 50
0
votes
1 answer
How to implement metaprogramming in c++ without std::enable_if
I was reading somewhere that it would be possible to do c++ metaprogramming using function overloads instead of SFINAE. I came up with a toy exercise to test this. I want to detect if a particular type is a nested vector. My code that works…

bradgonesurfing
- 30,949
- 17
- 114
- 217
0
votes
1 answer
How to generate a lookup table with sparse rows at compile time?
I have a list of entries (keyA, keyB, value) that I would like to have converted to a two-dimensional lookup table at compile time. However, due to the size of the data in question and the sparsity of its entries, it needs to be stored as an array…

AJMansfield
- 4,039
- 3
- 29
- 50
0
votes
1 answer
Is it OK to use lambda function parameter as a constant expression?
Why in this example the first call doesn't compile and the second one compiles?
consteval auto foo(auto x) {
static_assert(x);
}
int main(){
foo(42); // error: non-constant condition for static assertion
foo([]{}); // OK
}
If I…

cppbest
- 3
- 2
0
votes
0 answers
Return type of consteval method, based on method argument
Consider following code:
template
class foo {
[[nodiscard]]
consteval static int stoi(std::string_view) { return 1; /* dynamically determined */ }
consteval auto argOf(std::string_view i) ->…

Quest
- 2,764
- 1
- 22
- 44
0
votes
2 answers
Why isn't constexpr guaranteed to run during compilation?
Why isn't constexpr guaranteed to run during compilation?
Additionally, why was consteval added instead of changing constexpr to guarantee a compile-time execution?

user904963
- 1,520
- 2
- 15
- 33
0
votes
0 answers
in C++ can I define a constant pointer for GMP?
This seems like it should be simple but it doesn't seem to be. I know I can define a constant by using
#define magicNumber 20
But in my class I need to multiply and divide by 2 large numbers over and over and was trying to figure out the best way.…

Matthew Cornelisse
- 993
- 8
- 15
0
votes
0 answers
Why does constexpr not compute at compile-time for a string literal?
#include
#include
constexpr auto strlen_1(char const* sz) {
auto const* p = sz;
while (*p) {
++p;
}
return p - sz;
}
consteval auto strlen_2(char const* sz) {
auto const* p = sz;
while (*p) {
…

xmllmx
- 39,765
- 26
- 162
- 323