-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[371] - Did you know that C++26 added `= delete("should have a reason…
…")`?
- Loading branch information
1 parent
44cf780
commit 398fa2f
Showing
2 changed files
with
54 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
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> |