Skip to content

Commit c036064

Browse files
committed
map::operator[ ]とat : C++26対応として異種混合ルックアップに対応 #1189
1 parent 676aba0 commit c036064

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

reference/map/map/at.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,49 @@
66
* cpp11[meta cpp]
77

88
```cpp
9-
T& at(const key_type& x);
10-
const T& at(const key_type & x) const;
9+
T& at(const key_type& x); // (1) C++11
10+
const T& at(const key_type & x) const; // (2) C++11
11+
12+
template<class K>
13+
mapped_type& at(const K& x); // (3) C++26
14+
template<class K>
15+
const mapped_type& at(const K& x) const; // (4) C++26
1116
```
1217
1318
## 概要
1419
指定したキーを持つ要素を取得する。
15-
要素を取り出す際にキーの存在チェックをする。
20+
[`operator[]`](op_at.md)と違って、要素を取り出す際にキーに対応する値がなければ例外が送出される。
21+
22+
- (1), (2) : クラスのテンプレートパラメータ`key_type`型のキーに対応する要素を取得する
23+
- (3), (4) : `key_type`と比較可能な`K`型のキーに対応する要素を取得する
24+
25+
26+
## テンプレートパラメータ制約
27+
- (3), (4) : `key_compare::is_transparent` が妥当な式であること
28+
29+
30+
## 事前条件
31+
- (3), (4) : [`find`](find.md)`(x)`が妥当な式であり、定義された動作をすること
1632
1733
1834
## 戻り値
19-
キー`x`に対応する値を返す。対応する要素が存在しないときは、[`out_of_range`](/reference/stdexcept.md)例外を投げる。
35+
キー`x`に対応する値を返す。
2036
2137
2238
## 計算量
2339
要素数に対して対数時間
2440
2541
42+
## 例外
43+
- 対応する要素が存在しない場合、[`std::out_of_range`](/reference/stdexcept.md)例外を送出する
44+
45+
46+
## 備考
47+
- (3), (4) :
48+
- `is_transparent`は、標準ライブラリの[`std::less`](/reference/functional/less.md)、[`std::greater`](/reference/functional/greater.md)といった関数オブジェクトの、`void`に対する特殊化で定義される。それ以外のテンプレートパラメータで`is_transparent`が定義されないのは、互換性のためである。
49+
- これらのオーバーロードは、`map<string, int>`のようなコンテナに対し、検索操作で文字列リテラルを渡した際に、キー型の一時オブジェクトが生成されるコストを減らすためにある。
50+
51+
2652
## 例
2753
```cpp example
2854
#include <iostream>
@@ -78,4 +104,6 @@ exception std::out_of_range
78104

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

reference/map/map/op_at.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,23 @@
66

77
```cpp
88
T& operator[](const key_type& x); // (1) C++03
9+
910
T& operator[](key_type&& x); // (2) C++11
11+
12+
template <class K>
13+
T& operator[](K&& x); // (3) C++26
1014
```
1115

1216
## 概要
1317
指定したキーを持つ要素を取得する。対応する要素が存在しない場合は生成して返す。
1418

19+
- (1), (2) : クラスのテンプレートパラメータ`key_type`型のキーに対応する要素を取得する
20+
- (3) : `key_type`と比較可能な`K`型のキーに対応する要素を取得する
21+
22+
23+
## テンプレートパラメータ制約
24+
- (3) : `key_compare::is_transparent` が妥当な式であること
25+
1526

1627
## 戻り値
1728
キー`x`に対応する値を返す。対応する要素が存在しない場合は、要素を値初期化して参照を返す。
@@ -21,6 +32,12 @@ T& operator[](key_type&& x); // (2) C++11
2132
要素数に対して対数時間
2233

2334

35+
## 備考
36+
- (3) :
37+
- `is_transparent`は、標準ライブラリの[`std::less`](/reference/functional/less.md)[`std::greater`](/reference/functional/greater.md)といった関数オブジェクトの、`void`に対する特殊化で定義される。それ以外のテンプレートパラメータで`is_transparent`が定義されないのは、互換性のためである。
38+
- これらのオーバーロードは、`map<string, int>`のようなコンテナに対し、検索操作で文字列リテラルを渡した際に、キー型の一時オブジェクトが生成されるコストを減らすためにある。
39+
40+
2441
##
2542
```cpp example
2643
#include <iostream>
@@ -56,3 +73,6 @@ a
5673
| [`insert`](insert.md) | 要素を挿入する |
5774

5875

76+
## 参照
77+
- [P2363R5 Extending associative containers with the remaining heterogeneous overloads](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2363r5.html)
78+
- C++26で`template <class K>`のバージョンが追加された

0 commit comments

Comments
 (0)