constexpr
is similar to the const
specifier, the difference is that it is applicable to more types, eg also static member functions and so on.
Of course, like const
and auto
, it requires initialization at declaration time. In summary is:
The calculation is done at compile time gaining a much higher performance than if you didn’t use
constexpr
. Example of use:
To get runtime data you can use the command: time.
For more information on constexpr
see here .
consteval
?consteval
can be summarized as an improvement of constexpr
. Conceptually speaking, according to cppreference , it is a specifier that declares a function or model to be an immediate function .
This above sounded a bit difficult to understand, but in short it’s that the documentation refers to the order of execution.
constexpr
left “doubts” as to what the result would be after multiple uses for functions, programmers had that feeling that it ran at runtime, so consteval
was implemented from C++20 to resolve these discrepancies.
Below are some examples of using the consteval
Note that it is used for FUNCTIONS and only compiles with
-std=c++20
or higher, let your LSP know! 😃