Add constinit specifier.

This commit is contained in:
Anthony Calandra
2023-02-17 17:27:56 -05:00
parent 0dd38f0bad
commit e2f9058ca7
2 changed files with 22 additions and 0 deletions

View File

@@ -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