Skip to content

Commit ba21342

Browse files
committed
Add std::optional Range Support (#1431)
1 parent b403dd0 commit ba21342

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

reference/optional/optional.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
namespace std {
99
template <class T>
1010
class optional;
11+
12+
13+
// viewコンセプトを有効化する (C++26)
14+
template<class T>
15+
constexpr bool ranges::enable_view<optional<T>> = true;
16+
17+
// std::formatによるフォーマットを無効化する (C++26)
18+
template<class T>
19+
constexpr auto format_kind<optional<T>> = range_format::disabled;
1120
}
1221
```
1322
@@ -66,6 +75,14 @@ namespace std {
6675
| [`reset`](optional/reset.md) | 有効値を保持していない状態にする | C++17 |
6776
6877
78+
### イテレータ
79+
80+
| 名前 | 説明 | 対応バージョン |
81+
|------|------|----------------|
82+
| [`begin`](optional/begin.md) | `optional`をrangeとした時の先頭要素を指すイテレータを取得する | C++26 |
83+
| [`end`](optional/end.md) | `optional`をrangeとした時の末尾要素の次を指すイテレータを取得する | C++26 |
84+
85+
6986
### 値の観測
7087
7188
| 名前 | 説明 | 対応バージョン |
@@ -92,6 +109,8 @@ namespace std {
92109
| 名前 | 説明 | 対応バージョン |
93110
|------|------|----------------|
94111
| `value_type` | 要素型`T` | C++17 |
112+
| `iterator` | 実装定義のイテレータ型。[`contiguous_iterator`](/reference/iterator/contiguous_iterator.md)、[`random_access_iterator`](/reference/iterator/random_access_iterator.md)、constexprイテレータのモデルであり、コンテナのイテレータに対するすべての要件を満たす | C++26 |
113+
| `const_iterator` | 実装定義の読み取り専用イテレータ型。[`contiguous_iterator`](/reference/iterator/contiguous_iterator.md)、[`random_access_iterator`](/reference/iterator/random_access_iterator.md)、constexprイテレータのモデルであり、コンテナのイテレータに対するすべての要件を満たす | C++26 |
95114
96115
97116
## 非メンバ関数
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# begin
2+
* optional[meta header]
3+
* std[meta namespace]
4+
* optional[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
constexpr iterator begin() noexcept; // (1)
10+
constexpr const_iterator begin() const noexcept; // (2)
11+
```
12+
13+
## 概要
14+
`optional`をrangeとした時の先頭要素を指すイテレータを取得する。
15+
16+
17+
## 効果
18+
[`has_value()`](has_value.md) `== true`なら、保持している有効値を指すイテレータを返す。またこの時、[`distance`](/reference/iterator/distance.md)`(begin(),` [`end()`](end.md)`)` `== 1` となる。
19+
20+
[`has_value()`](has_value.md) `!= true`なら、末尾要素の次を指すイテレータを返す。この時、`begin() ==` [`end()`](end.md) である。
21+
22+
23+
##
24+
```cpp example
25+
#include <iostream>
26+
#include <optional>
27+
28+
int main()
29+
{
30+
std::optional<int> p1 = 1;
31+
for (auto i = p1.begin(); i != p1.end(); ++i) {
32+
std::cout << *i; // 1度通る
33+
}
34+
35+
std::optional<int> p2 = std::null_opt;
36+
for (auto i = p2.begin(); i != p2.end(); ++i) {
37+
std::cout << *i; // 1度も通らない
38+
}
39+
}
40+
```
41+
* begin()[color ff0000]
42+
* end()[link end.md]
43+
44+
### 出力
45+
```
46+
1
47+
```
48+
49+
50+
## バージョン
51+
### 言語
52+
- C++26
53+
54+
### 処理系
55+
- [Clang](/implementation.md#clang): ??
56+
- [GCC](/implementation.md#gcc): ??
57+
- [ICC](/implementation.md#icc): ??
58+
- [Visual C++](/implementation.md#visual_cpp): ??
59+
60+
61+
## 参照
62+
- [P3168R2 Give std::optional Range Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3168r2.html)

reference/optional/optional/end.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# end
2+
* optional[meta header]
3+
* std[meta namespace]
4+
* optional[meta class]
5+
* function[meta id-type]
6+
* cpp26[meta cpp]
7+
8+
```cpp
9+
constexpr iterator end() noexcept; // (1)
10+
constexpr const_iterator end() const noexcept; // (2)
11+
```
12+
13+
## 概要
14+
`optional`をrangeとした時の末尾要素の次を指すイテレータを取得する。
15+
16+
17+
## 効果
18+
[`begin()`](begin.md) `+` [`has_value()`](has_value.md) を返す。
19+
20+
21+
##
22+
```cpp example
23+
#include <iostream>
24+
#include <iterator>
25+
#include <optional>
26+
27+
int main()
28+
{
29+
std::optional<int> p = 1;
30+
std::cout << std::distance(p.begin(), p.end()) << std::endl;
31+
32+
p = std::null_opt;
33+
std::cout << std::distance(p.begin(), p.end()) << std::endl;
34+
}
35+
```
36+
* end()[color ff0000]
37+
* begin()[link begin.md]
38+
* std::distance[link /reference/iterator/distance.md]
39+
40+
### 出力
41+
```
42+
1
43+
0
44+
```
45+
46+
47+
## バージョン
48+
### 言語
49+
- C++26
50+
51+
### 処理系
52+
- [Clang](/implementation.md#clang): ??
53+
- [GCC](/implementation.md#gcc): ??
54+
- [ICC](/implementation.md#icc): ??
55+
- [Visual C++](/implementation.md#visual_cpp): ??
56+
57+
58+
## 参照
59+
- [P3168R2 Give std::optional Range Support](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3168r2.html)

0 commit comments

Comments
 (0)