Skip to content

Commit

Permalink
[371] - Did you know that C++26 added `= delete("should have a reason…
Browse files Browse the repository at this point in the history
…")`?
  • Loading branch information
krzysztof-jusiak committed Apr 16, 2024
1 parent 44cf780 commit 398fa2f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

> C++26
* [Did you know that C++26 added `= delete("should have a reason")`?](https://github.com/tip-of-the-week/cpp/blob/master/tips/371.md)
* [Did you know that C++26 added `span.at`?](https://github.com/tip-of-the-week/cpp/blob/master/tips/370.md)
* [Did you know about C++26 simd proposal (1/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/367.md)
* [Did you know about C++26 static reflection proposal (1/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/361.md)
Expand Down
53 changes: 53 additions & 0 deletions tips/371.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<details open><summary>Info</summary><p>

* **Did you know that C++26 added `= delete("should have a reason")`?**

* https://wg21.link/P2573

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

```cpp
void newapi();
void oldapi() = delete("This old API is outdated and already been removed. Please use newapi() instead.");

int main () {
oldapi();
}
```
> https://godbolt.org/z/aPz1bM4x6
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implment simplified addressof with `delete("Cannot take address of rvalue.")` when used incorreclty?**
```cpp
// TODO addressof
int main() {
int i{};
addressof(i); // okay
addressof(int{}); // error: "Cannot take address of rvalue."
}
```

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

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

```cpp
template<class T> constexpr T* addressof(T& r) noexcept { return &r; }
template<class T> const T* addressof(const T&&) = delete("Cannot take address of rvalue.");

int main() {
int i{};
addressof(i); // okay
addressof(int{}); // error: "Cannot take address of rvalue."
}
```
> https://godbolt.org/z/hMYWThGxj
</p></details>

0 comments on commit 398fa2f

Please sign in to comment.