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
80
votes
1 answer

What is consteval?

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?
KevinZ
  • 3,036
  • 1
  • 18
  • 26
34
votes
1 answer

New-expression with consteval constructor in constexpr context

struct A { consteval A() {}; }; constexpr bool g() { auto a = new A; delete a; return true; } int main() { static_assert(g()); } https://godbolt.org/z/jsq35WxKs GCC and MSVC reject the program, ICC and Clang accept…
user17732522
  • 53,019
  • 2
  • 56
  • 105
24
votes
3 answers

Will consteval allow using static_assert on function arguments?

Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr. That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some…
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
19
votes
1 answer

Why does a consteval function allow undefined behavior?

There is a very neat property of constant expressions in C++: their evaluation cannot have undefined behavior (7.7.4.7): An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
18
votes
1 answer

What is if consteval needed for?

C++23 is going to introduce if consteval. Where is this going to be used and how does it differ from constexpr if?
Claas Bontus
  • 1,628
  • 1
  • 15
  • 29
18
votes
1 answer

consteval vs constexpr on variables

Is there any difference between constexpr and consteval ? consteval int x1 = 2; constexpr int x2 = 5; Is it better to use constexpr than consteval?
Ionut Alexandru
  • 680
  • 5
  • 17
14
votes
2 answers

Is compiler allowed to call an immediate (consteval) function during runtime?

This might be a stupid question, but I am confused. I had a feeling that an immediate (consteval) function has to be executed during compile time and we simply cannot see its body in the binary. This article clearly supports my feeling: This has…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
13
votes
1 answer

Cannot use consteval functions inside initializer list on MSVC

Consider that minimized code snippet: #include class Bar { public: constexpr Bar() {} }; consteval Bar foo() { return Bar(); } int main() { std::vector bars{ foo(), foo() }; } This doesn't compile on latest MSVC…
prapin
  • 6,395
  • 5
  • 26
  • 44
11
votes
1 answer

Can a class with consteval constructor be created on heap in C++?

In the following code struct A has immediate function default constructor, and an object of the struct is created in the dynamic memory be means of new A{}: struct A { consteval A() {} }; int main() { new A{}; } Only Clang accepts…
Fedor
  • 17,146
  • 13
  • 40
  • 131
10
votes
1 answer

Non-literal types and constant expressions

struct A { ~A() {} consteval A() {} consteval auto f() {} }; int main() { A{}; //A{}.f(); //1 } https://godbolt.org/z/4KPY5P7o7 This program is accepted by ICC, GCC and Clang, but rejected by MSVC which complains that…
user17732522
  • 53,019
  • 2
  • 56
  • 105
10
votes
2 answers

Can `consteval` delegating constructor be called from an ordinary constructor?

In the following struct definition, the constructor A(int) delegates its work to immediate-function constructor A(): struct A { int i = 0; consteval A() = default; A(int) : A() {} }; Clang accepts it, but not GCC…
Fedor
  • 17,146
  • 13
  • 40
  • 131
10
votes
1 answer

Are `inline` and `noexcept` redundant in a consteval context?

I am working with some code in which constexpr functions are used which I currently refactor to be consteval whenever possible. inline constexpr auto example() noexcept { /*...*/ } As I understand it, the inline keyword above is already redundant…
mutableVoid
  • 1,284
  • 2
  • 11
  • 29
8
votes
1 answer

Returning std::vector from an immediate function

Since C++20, std::vector can be used in constant expressions. And as far as I known, current C++ permits dynamically allocate memory under the condition that any such allocation is deallocated by the time the constant expression is "over". However,…
Fedor
  • 17,146
  • 13
  • 40
  • 131
8
votes
1 answer

Must consteval constructor initialize all data members?

In the next program struct B has immediate consteval default constructor, which does not initialize i field. Then this constructor is used to make a temporary and its i field remains untouched: struct B { bool b = true; int i; consteval…
Fedor
  • 17,146
  • 13
  • 40
  • 131
8
votes
2 answers

Why can't we use compile-time 'variables' in consteval functions as template parameters?

I was testing this code (https://godbolt.org/z/fe6hhbeqW)... // Returns the nth type in a parameter pack of types (ommited for clarity) // template // nth_type{} template struct Typelist{ …
ronaldo
  • 455
  • 3
  • 9
1
2 3 4 5 6