Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 209
KevinZ Avatar asked Nov 17 '18 01:11

KevinZ


People also ask

Why use consteval?

C++20 introduces the keyword consteval, which is used to indicate that a function must evaluate at compile-time, otherwise a compile error will result. Such functions are called immediate functions. In the above example, the first two calls to greater() will evaluate at compile-time.

When to use consteval vs constexpr?

A consteval function can only invoke a constexpr function but not the other way around. use fundamental data types as variables that have to be initialized with a constant expression. A consteval (constexpr) function cannot. have static or thread_local data.

What is a constexpr function?

A constexpr function is a function that can be invoked within a constant expression. A constexpr function must satisfy the following conditions: It is not virtual. Its return type is a literal type. Each of its parameters must be of a literal type.

What is Constinit?

constinit - asserts that a variable has static initialization, i.e. zero initialization and constant initialization, otherwise the program is ill-formed.


1 Answers

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).

like image 87
T.C. Avatar answered Sep 26 '22 02:09

T.C.