Skip to content

Commit

Permalink
[356] - Did you know that C++20's no_unique_address can be used to …
Browse files Browse the repository at this point in the history
…find unique types?
  • Loading branch information
krzysztof-jusiak committed Nov 13, 2023
1 parent c9602ad commit b5554fa
Show file tree
Hide file tree
Showing 3 changed files with 53 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's `no_unique_address` can be used to find unique types?](https://github.com/tip-of-the-week/cpp/blob/master/tips/356.md)
* [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)
Expand Down
7 changes: 7 additions & 0 deletions tips/355.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ extern constinit thread_local int var;
</p></details>

```cpp
extern constinit thread_local int var;
auto fn() { return var; }
```
> https://godbolt.org/z/rshcj6Tq5
</p></details><details><summary>Solutions</summary><p>
</p></details>
45 changes: 45 additions & 0 deletions tips/356.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<details open><summary>Info</summary><p>

* **Did you know that C++20's `no_unique_address` can be used to find unique types?**

* https://wg21.link/P0840

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

```cpp
template<class> struct box{};

struct unique {
[[no_unique_address]] box<int> _1;
[[no_unique_address]] box<bool> _2;
};
static_assert(sizeof(unique)==1);

struct no_unique {
[[no_unique_address]] box<int> _1;
[[no_unique_address]] box<int> _2;
};

static_assert(sizeof(no_unique)!=1);
```
> https://godbolt.org/z/Mdsa16rWT
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement generic version of `is_unique` leveraging described concept?**
```cpp
template <class... Ts>
constexpr auto is_unique = false; // TODO
static_assert(is_unique<int, bool, char, double, float>);
static_assert(not is_unique<int, bool, char, double, float, int>);
```

> https://godbolt.org/z/Exjdf4sbx

</p></details>

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

0 comments on commit b5554fa

Please sign in to comment.