From 398fa2f2ae7edc8b91ff3114d2f68f4002582600 Mon Sep 17 00:00:00 2001 From: Kris Jusiak Date: Tue, 16 Apr 2024 04:10:12 -0500 Subject: [PATCH] [371] - Did you know that C++26 added `= delete("should have a reason")`? --- README.md | 1 + tips/371.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tips/371.md diff --git a/README.md b/README.md index 72264b2..c415437 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/tips/371.md b/tips/371.md new file mode 100644 index 0000000..13f4644 --- /dev/null +++ b/tips/371.md @@ -0,0 +1,53 @@ +
Info

+ +* **Did you know that C++26 added `= delete("should have a reason")`?** + + * https://wg21.link/P2573 + +

Example

+ +```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 + +

Puzzle

+ +* **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 + +

+ +

Solutions

+ +```cpp +template constexpr T* addressof(T& r) noexcept { return &r; } +template 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 + +