From e2f9058ca7961fb97b1f4308256ca8d61107a9b9 Mon Sep 17 00:00:00 2001 From: Anthony Calandra Date: Fri, 17 Feb 2023 17:27:56 -0500 Subject: [PATCH] Add constinit specifier. --- CPP20.md | 11 +++++++++++ README.md | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/CPP20.md b/CPP20.md index 22ddecf..ae26270 100644 --- a/CPP20.md +++ b/CPP20.md @@ -18,6 +18,7 @@ C++20 includes the following new language features: - [using enum](#using-enum) - [lambda capture of parameter pack](#lambda-capture-of-parameter-pack) - [char8_t](#char8_t) +- [constinit](#constinit) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -435,6 +436,16 @@ Provides a standard type for representing UTF-8 strings. char8_t utf8_str[] = u8"\u0123"; ``` +### constinit +The `constinit` specifier requires that a variable must be initialized at compile-time. +```c++ +const char* g() { return "dynamic initialization"; } +constexpr const char* f(bool p) { return p ? "constant initializer" : g(); } + +constinit const char* c = f(true); // OK +constinit const char* d = f(false); // ERROR: `g` is not constexpr, so `d` cannot be evaluated at compile-time. +``` + ## C++20 Library Features ### Concepts library diff --git a/README.md b/README.md index 8f4ad46..82132c6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ C++20 includes the following new language features: - [using enum](#using-enum) - [lambda capture of parameter pack](#lambda-capture-of-parameter-pack) - [char8_t](#char8_t) +- [constinit](#constinit) C++20 includes the following new library features: - [concepts library](#concepts-library) @@ -536,6 +537,16 @@ Provides a standard type for representing UTF-8 strings. char8_t utf8_str[] = u8"\u0123"; ``` +### constinit +The `constinit` specifier requires that a variable must be initialized at compile-time. +```c++ +const char* g() { return "dynamic initialization"; } +constexpr const char* f(bool p) { return p ? "constant initializer" : g(); } + +constinit const char* c = f(true); // OK +constinit const char* d = f(false); // ERROR: `g` is not constexpr, so `d` cannot be evaluated at compile-time. +``` + ## C++20 Library Features ### Concepts library