-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[356] - Did you know that C++20's
no_unique_address
can be used to …
…find unique types?
- Loading branch information
1 parent
c9602ad
commit b5554fa
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |