Questions tagged [consteval]

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.

79 questions
2
votes
2 answers

Is it possible to pass a reference to a consteval function and use it as additional return value?

Sometimes the result of a function can not be represented by a single return value. For example: A function that intersects two lines. One might want the function to return both the actual point of intersection as well as their relation to each…
hanslhansl
  • 63
  • 4
2
votes
2 answers

how to guarantee initilization of a stack variable with a compile time constant

In C++20 we now have constinit. constexpr and consteval. I can now guarantee that a static variable is initialized by the result of a constexpr or consteval function by using constinit. OK I also can guarantee that a stack variable is initialized…
Klaus
  • 24,205
  • 7
  • 58
  • 113
2
votes
2 answers

Is it possible to call a consteval function with a non-const reference parameter?

If I understood the rules for immediate functions correctly, the following is a legal usage: consteval void func(int& a) { /* Anything here... */ } Is it possible to call this function? I could not find any way to do so as consteval forces that…
Brotcrunsher
  • 1,964
  • 10
  • 32
2
votes
1 answer

Compile-time init a NON constant variable

Question: How do I compile-time initialize a non-constant variable with a function while still enabling a runtime call to said function? Details: I am using C++20, and I have the following code: template constexpr auto diag(auto&&..…
Adelhart
  • 395
  • 2
  • 11
2
votes
1 answer

Is there any way of accessing arbitrary data of known size as a char array in a constexpr/consteval context?

I'm trying to implement something that will take in arbitrary bits of data (which are known at compile time) and calculate their CRC as a consteval , so I can use it to e.g. index such data with integer keys without any runtime overhead. I have it…
tohoho
  • 339
  • 1
  • 10
2
votes
0 answers

When to use constinit and consteval?

The usage of constexpr is quite straightforward which is to make sure the code can be evaluated at the compile time itself. But the latest features of C++ 20 which provides constinit and consteval are little bit confusing. Can someone provide the…
ashubhatt
  • 21
  • 2
2
votes
1 answer

A name for describing `consteval` function argument being known at compile time but not constexpr

Argument of consteval function is: sort of known at compile time but is not a constexpr The motivation behind this behavior is explained by Andrew Sutton in his paper Translation and evaluation: A mental model for compile-time metaprogramming, as…
Amir Kirsh
  • 12,564
  • 41
  • 74
1
vote
2 answers

What are the advantages of using constexpr instead of consteval function?

This might seem the same question as What are the advantages of using consteval instead of constexpr function? but it is actually exactly the opposite: Now that we have C++20 consteval, what are the (realistic) scenarios for which I would still…
DanRechtsaf
  • 518
  • 3
  • 8
1
vote
2 answers

Is it possible to raise compile error when string is too long without macros in C++20 or lower?

I am writing a simple fixed length string struct. In runtime, if you assign strings that are too long, I will just silently truncate them. This is for embedded and the string are to be displayed on a display of limited size, so they would get…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
1
vote
2 answers

custom minimalist std::map with constexpr operator[]

I am currently working on implementing a map container in C++, which should be able to function as a compile-time constant. More specifically, my intention is to create a static, pre-defined lookup table. In this table, a key-value pair should…
user23952
  • 578
  • 3
  • 10
1
vote
2 answers

typeid().name() and consteval function problem

I am using MSVC 19, and get an error while compiling the following code. Is there any way to get type_info and name of the object in consteval functions? class CChTest { public: static consteval const char* test() { …
VeNToR
  • 57
  • 1
  • 4
1
vote
1 answer

string literals as template arguments forces code duplication and verbosity

A compile-time created string is being used as a character array in a structure and its max size is driven by that string. Also that structure is being used as a member variable in another struct, and there are multiple of such string buffer structs…
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
1
vote
1 answer

Why can't I pass const array references to nested consteval functions?

So I've got this code (compiling with clang x86-64 14.0.0): #include template struct container { char data[size]; }; template consteval size_t func2(const char (&string)[n]) { return 1; } template
1
vote
1 answer

Cast pointers in consteval

I wanted to make a compiletime data encryptor. I tried this by creating a struct with a buffer, and a constructor for that struct that would read an array of structures in that buffer. And because I wanted to use blockcypher, I wanted that buffer…
1
vote
1 answer

undefined reference to `std::is_constant_evaluated()' in c++20 gcc

This is just a minimal reproducible example: import ; import ; template class my_class { }; template struct std::hash> { std::size_t operator()(my_class const &s) const noexcept { …
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35