Open In App

constinit Specifier in C++ 20

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The constinit specifier is a new feature that is introduced in C++ 20 that allows us to declare a variable with static storage duration. In this article we will discuss the constinit specifier, its usage, advantages, and limitations.

What is constinit Specifier?

The constinit specifier is used to mark variables that must be initialized with compile-time constant expressions or constant-initialized constructors and it also ensures that the initialization will be done during the static initialization phase. It prevents the variables with static storage duration to be initialized at runtime. The variables specified with constinit specifier need to be initialized with a constant expression.

Note: constinit cannot be used together with constexpr or consteval as constinit is used for static initialization of variables, which happens before the program starts the execution, whereas constexpr and consteval are used to evaluate the expression at compile time.

Syntax

constinit T variable = initializer;

Where,

  • T: It indicates the type of variable
  • variable: It is the name of the variable
  • initializer: It is the constant expression that is used for the initialization.

Examples of constinit

Example 1:

In the below code, we will make use of the above syntax to demonstrate the use of the Constinit Specifier.

C++




// C++ program to illustrate the constinit specifiers
#include <iostream>
using namespace std;
  
// Declare a constinit variable
constinit int x = 42;
  
int main()
{
    cout << "x = " << x << endl;
    return 0;
}


Output

x = 42

Example 2:

Below is the code that demonstrates that constinit cannot be used together with constexpr in C++.

C++




// C++ program to illustrate the restrictions on constinit
#include <iostream>
using namespace std;
  
// Error: constinit cannot be used with constexpr
constinit constexpr int x = 42;
  
int main()
{
    cout << x << std::endl;
    return 0;
}


Error

 

Example 3:

Below is the code that demonstrates that constinit cannot be used together with consteval in C++.

C++




// C++ program to illustrate the restrictions on constinit
#include <iostream>
using namespace std;
  
// Error: constinit cannot be used with consteval
constinit consteval int square(int x) { return x * x; }
  
int main()
{
    cout << square(5) << std::endl;
    return 0;
}


Error

 

Advantages of constinit

The benefits of using the Constinit Specifier in C++ 20 are:

  • Reliability: This specifier ensures that the variables are initialized in a controlled manner that reduces the chances of unexpected behavior due to uninitialized and partially initialized variables.
  • Static initialization order control: The programmer can manage the order of initialization for the variable which will reduce the problem related to the static initialization order.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads