mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-16 17:47:02 +03:00
Add constinit specifier.
This commit is contained in:
11
CPP20.md
11
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
|
||||
|
||||
11
README.md
11
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
|
||||
|
||||
Reference in New Issue
Block a user