Skip to content

Commit

Permalink
[355] - Did you know that C++20 added constinit keyword?
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Nov 6, 2023
1 parent 93d87bf commit c4a9127
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@

> C++20
* [Did you know that C++20 added constinit keyword?](https://github.com/tip-of-the-week/cpp/blob/master/tips/355.md)
* [Did you know about C++20 `std::next_permutation` algorithm?](https://github.com/tip-of-the-week/cpp/blob/master/tips/338.md)
* [Did you know that C++20 added `std::span`?](https://github.com/tip-of-the-week/cpp/blob/master/tips/333.md)
* [Did you know that with C++20 you can pass concepts?](https://github.com/tip-of-the-week/cpp/blob/master/tips/317.md)
Expand Down
16 changes: 16 additions & 0 deletions tips/354.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ static_assert(&b.data[42] == std::string_view{b}.end());
</p></details>

</p></details><details><summary>Solutions</summary><p>

```cpp
template <auto N>
struct buffer {
constexpr buffer() {}
constexpr char const* begin() const { return data; }
constexpr char const* end() const { return data + N; }
constexpr operator std::basic_string_view<char>() const {
return std::basic_string_view<char>(begin(), end());
}
char data[N]{};
};
```
> https://godbolt.org/z/1cY8enTqc
</p></details>
28 changes: 28 additions & 0 deletions tips/355.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<details open><summary>Info</summary><p>

* **Did you know that C++20 added constinit keyword?**

* https://wg21.link/P1143

</p></details><details open><summary>Example</summary><p>

```cpp
constinit static auto i = 42;
```

> https://godbolt.org/z/8xWqf9vE8
</p></details><details open><summary>Puzzle</summary><p>

* **Can you implement example showing constinit with thread_local storage?**

```cpp
extern constinit thread_local int var;
```

> https://godbolt.org/z/zznznchEE
</p></details>

</p></details><details><summary>Solutions</summary><p>
</p></details>

0 comments on commit c4a9127

Please sign in to comment.