Skip to content

Commit 302be0f

Browse files
committed
map::insert_or_assign : C++26の異種混合ルックアップに対応 #1189
1 parent c057325 commit 302be0f

2 files changed

Lines changed: 41 additions & 19 deletions

File tree

reference/map/map/at.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,5 @@ exception std::out_of_range
104104

105105
## 参照
106106
- [LWG Issue 464. Suggestion for new member functions in standard containers](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#464)
107-
## 参照
108107
- [P2363R5 Extending associative containers with the remaining heterogeneous overloads](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2363r5.html)
109108
- C++26で`template <class K>`のバージョンが追加された

reference/map/map/insert_or_assign.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,33 @@
77

88
```cpp
99
template <class M>
10-
pair<iterator, bool> insert_or_assign(const key_type& k, M&& obj); // (1)
11-
10+
pair<iterator, bool>
11+
insert_or_assign(const key_type& k,
12+
M&& obj); // (1) C++17
1213
template <class M>
13-
pair<iterator, bool> insert_or_assign(key_type&& k, M&& obj); // (2)
14-
14+
pair<iterator, bool>
15+
insert_or_assign(key_type&& k,
16+
M&& obj); // (2) C++17
1517
template <class M>
16-
iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); // (3)
17-
18+
iterator
19+
insert_or_assign(const_iterator hint,
20+
const key_type& k,
21+
M&& obj); // (3) C++17
1822
template <class M>
19-
iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); // (4)
23+
iterator
24+
insert_or_assign(const_iterator hint,
25+
key_type&& k,
26+
M&& obj); // (4) C++17
27+
28+
template <class K, class M>
29+
pair<iterator, bool>
30+
insert_or_assign(K&& k,
31+
M&& obj); // (5) C++26
32+
template <class K, class M>
33+
iterator
34+
insert_or_assign(const_iterator hint,
35+
K&& k,
36+
M&& obj); // (6) C++26
2037
```
2138
* pair[link /reference/utility/pair.md]
2239
@@ -29,40 +46,44 @@ iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj);
2946
- (2) : `key_type`型の一時オブジェクトのキーをとって挿入もしくは代入する
3047
- (3) : 挿入位置のヒントをともない、`key_type`型のキーをとって挿入もしくは代入する
3148
- (4) : 挿入位置のヒントをともない、`key_type`型の一時オブジェクトのキーをとって挿入もしくは代入する
49+
- (5) : `key_type`と比較可能な`K`型のキーをとって挿入もしくは代入する
50+
- (6) : 挿入位置のヒントをともない、`key_type`と比較可能な`K`型のキーをとって挿入もしくは代入する
3251
3352
3453
## テンプレートパラメータ制約
35-
- (1)、(3) : [`is_assignable_v`](/reference/type_traits/is_assignable.md)`<mapped_type&, M&&>` が `true` であること。`value_type` は、`k`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から `map` に直接構築可能であること
54+
- (5), (6) : `key_compare::is_transparent` が妥当な式であること
55+
56+
57+
## 適格要件
58+
- (1)、(3)、(5)、(6) : [`is_assignable_v`](/reference/type_traits/is_assignable.md)`<mapped_type&, M&&>` が `true` であること。`value_type` は、`k`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から `map` に直接構築可能であること
3659
- (2)、(4) : [`is_assignable_v`](/reference/type_traits/is_assignable.md)`<mapped_type&, M&&>` が `true` であること。`value_type` は、[`move`](/reference/utility/move.md)`(k)`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から `map` に直接構築可能であること
3760
3861
なお、規格に記載はないが、`hint` は [`emplace_hint`](emplace_hint.md) と同様、コンテナの有効な読み取り専用イテレータである必要があるものと思われる。
3962
4063
4164
## 効果
42-
- (1)、(3) : `map` が `k` と同値のキーを持つ要素 `e` を持っている場合、`e.second` に [`forward`](/reference/utility/forward.md)`<M>(obj)` を代入する。そうでなければ、`k`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から構築した `value_type` 型のオブジェクトを挿入する。
65+
- (1)、(3)、(5)、(6) : `map` が `k` と同値のキーを持つ要素 `e` を持っている場合、`e.second` に [`forward`](/reference/utility/forward.md)`<M>(obj)` を代入する。そうでなければ、`k`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から構築した `value_type` 型のオブジェクトを挿入する。
4366
- (2)、(4) : `map` が `k` と同値のキーを持つ要素 `e` を持っている場合、`e.second` に [`forward`](/reference/utility/forward.md)`<M>(obj)` を代入する。そうでなければ、[`move`](/reference/utility/move.md)`(k)`, [`forward`](/reference/utility/forward.md)`<M>(obj)` から構築した `value_type` 型のオブジェクトを挿入する。
4467
4568
4669
## 戻り値
47-
- (1)、(2) : イテレータと `bool` 値の [`pair`](/reference/utility/pair.md) を返す。
70+
- (1)、(2)、(5) : イテレータと `bool` 値の [`pair`](/reference/utility/pair.md) を返す。
4871
- 挿入された場合には、`first` に挿入された要素へのイテレータ、`second` に `true` が設定される。
4972
- 代入された場合には、`first` に代入された要素へのイテレータ、`second` に `false` が設定される。
50-
- (3)、(4) :
73+
- (3)、(4)、(6) :
5174
- 挿入された場合には、挿入された要素へのイテレータを返す。
5275
- 代入された場合には、代入された要素へのイテレータを返す。
5376
5477
5578
## 計算量
56-
- (1)、(2) : [`emplace`](emplace.md) と同じ。
57-
- (3)、(4) : [`emplace_hint`](emplace_hint.md) と同じ。
79+
- (1)、(2)、(5) : [`emplace`](emplace.md) と同じ。
80+
- (3)、(4)、(6) : [`emplace_hint`](emplace_hint.md) と同じ。
5881
5982
6083
## 備考
61-
このメンバ関数の機能テストマクロは以下の通り。
62-
63-
| マクロ名 | 値 |
64-
|-----------------------------|----------|
65-
| `__cpp_lib_map_try_emplace` | `201411` |
84+
- (5), (6) :
85+
- `is_transparent`は、標準ライブラリの[`std::less`](/reference/functional/less.md)、[`std::greater`](/reference/functional/greater.md)といった関数オブジェクトの、`void`に対する特殊化で定義される。それ以外のテンプレートパラメータで`is_transparent`が定義されないのは、互換性のためである。
86+
- これらのオーバーロードは、`map<string, int>`のようなコンテナに対し、検索操作で文字列リテラルを渡した際に、キー型の一時オブジェクトが生成されるコストを減らすためにある。
6687
6788
6889
## 例
@@ -116,3 +137,5 @@ int main()
116137
- [N4006 An improved emplace() for unique-key maps](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4006.html)
117138
- [N4240 Improved insertion interface for unique-key maps (Revision 2)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4240.html)
118139
- [N4279 Improved insertion interface for unique-key maps (Revision 2.3)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4279.html)
140+
- [P2363R5 Extending associative containers with the remaining heterogeneous overloads](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2363r5.html)
141+
- C++26で`template <class K>`のバージョンが追加された

0 commit comments

Comments
 (0)