Skip to content

Latest commit

 

History

History
138 lines (121 loc) · 2.45 KB

consteval_constinit.md

File metadata and controls

138 lines (121 loc) · 2.45 KB

New Keywords - consteval and constinit

consteval

A function specified with the keyword consteval is an immediate function that is executed at compile-time.
Each call to this function must produce a compile-time constant expression.

This immediate function must satisfy all requirements applicable to constexpr functions.

C++14 - constexpr function C++20 - consteval function
// executes at compile-time or run-time
constexpr int squareNumX(int n) 
{
    return n * n;
}
// executes at compile-time
consteval int squareNumV(int n) 
{
    return n * n;
}

Immediate functions (specified with consteval) cannot be applied to:

  • destructors
  • functions which allocate or deallocate
C++14 C++20
{
    int x = 100;
    const int y = 100;  
    int a = squareNumX(x);
    constexpr int b = squareNumX(x); // ERROR
    int c = squareNumX(y);
    constexpr int d = squareNumX(y);
}
// Error when x is not a constant expression but b is

{
    int x = 100;
    const int y = 100;
    int a = squareNumV(x);              // ERROR
    constexpr int b = squareNumV(x);    // ERROR
    int c = squareNumV(y);
    constexpr int d = squareNumV(y);  
}
// Error when the function argument (x) is not a constant expression 

constinit

Applied to variables with static storage duration or thread storage duration.
constinit variables are initialized at compile-time.

constinit does not imply constness such as const or constexpr(2).

C++14 - constexpr C++20 - constinit
// static storage duration
constexpr int a = 100;  
int main() 
{
      ++a;                      // ERROR 
      constexpr auto b = 100;   
}
// Error since constexpr or const cannot be modifed 
// constexpr or const can be created locally
// static storage duration
constinit int a = 100;  
int main()
{  
      ++a; 
      constinit auto b = 100;  // ERROR
      // b has thread storage duration
      constinit thread_local auto b = 100;
}
// Error since constinit cannot be created locally
// constinit can be modified