Skip to content

Commit

Permalink
Add constinit specifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyCalandra committed Feb 17, 2023
1 parent 0dd38f0 commit e2f9058
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CPP20.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e2f9058

Please sign in to comment.