Skip to content

Commit 7e168a5

Browse files
committed
add deducingthis exam
1 parent 383965d commit 7e168a5

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

docs/error_code.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[TOC]
44

5+
## 为什么错误很重要?
6+
57
## 提前返回是好习惯!
68

79
TODO
@@ -14,4 +16,4 @@ TODO
1416

1517
## `std::expected`
1618

17-
`boost::expected`
19+
也可以 `boost::expected` 替代。

docs/stl_map.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,23 @@ V const &at(map<K, V> const *this, K const &k); // 第二个版本的 at
911911
- 当 map 对象为 const 时,传入的 this 指针为 `map<K, V> const *`,所以只能调用第二个版本的 at。
912912
- 当 map 对象不为 const 时,传入的 this 指针为 `map<K, V> *`,两个重载都可以调用,但由于第一个重载更加符合,所以会调用第一个版本的 at。
913913

914+
> {{ icon.detail }} 有趣的是,C++23 支持了显式对象形参(deducing-this),this 也能像普通参数一样定义了!上面的代码可以写成:
915+
916+
```cpp
917+
class map {
918+
...
919+
920+
V &at(this map &self, K const &k) {
921+
// 函数体内可以使用self代替原来的this(this将不再可用)
922+
...
923+
}
924+
925+
V const &at(this map const &self, K const &k) {
926+
...
927+
}
928+
};
929+
```
930+
914931
---
915932

916933
<!-- PG38 -->

0 commit comments

Comments
 (0)