Skip to content

Commit

Permalink
make_const_iterator/make_const_sentinel追加 #1023
Browse files Browse the repository at this point in the history
  • Loading branch information
onihusube committed Aug 10, 2023
1 parent 5ba1413 commit 396b2b6
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
4 changes: 2 additions & 2 deletions reference/iterator/basic_const_iterator.md
Expand Up @@ -78,8 +78,8 @@ namespace std {
| 名前 | 説明 | 対応バージョン |
|------------------------------------------------------|-------------|-------|
| [`make_const_iterator`](make_const_iterator.md.nolink) | `basic_const_iterator`のヘルパ関数 | C++23 |
| [`make_const_sentinel`](make_const_sentinel.md.nolink) | `basic_const_iterator`のヘルパ関数 | C++23 |
| [`make_const_iterator`](make_const_iterator.md) | `basic_const_iterator`のヘルパ関数 | C++23 |
| [`make_const_sentinel`](make_const_sentinel.md) | `basic_const_iterator`のヘルパ関数 | C++23 |
## 例
Expand Down
66 changes: 66 additions & 0 deletions reference/iterator/make_const_iterator.md
@@ -0,0 +1,66 @@
# make_const_iterator
* iterator[meta header]
* std[meta namespace]
* function[meta id-type]
* cpp23[meta cpp]

```cpp
namespace std {
template<input_iterator I>
constexpr const_iterator<I> make_const_iterator(I it);
}
```
* input_iterator[link input_iterator.md]
* const_iterator[link const_iterator.md.nolink]
## 概要
`basic_const_iterator`のヘルパ関数。
## 戻り値
```cpp
return it;
```

戻り値型は必ずしも`basic_const_iterator`の特殊化になるわけではない。

##
```cpp example
#include <iostream>
#include <vector>
#include <memory>
#include <iterator>

int main() {
std::vector vec = {1, 2, 3, 4, 5};

auto cit = std::make_const_iterator(vec.begin());
auto cse = std::make_const_sentinel(vec.end());

for (auto& n : std::ranges::subrange{cit, cse}) {
std::cout << n << ", ";
// 変更できない
// n = 0;
}
}
```
* std::make_const_iterator[color ff0000]
* make_const_sentinel[link make_const_sentinel.md]

### 出力
```
1, 2, 3, 4, 5,
```

## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): 13.1
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 6

## 参照

- [P2278R4 `cbegin` should always return a constant iterator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2278r4.html)
70 changes: 70 additions & 0 deletions reference/iterator/make_const_sentinel.md
@@ -0,0 +1,70 @@
# make_const_sentinel
* iterator[meta header]
* std[meta namespace]
* function[meta id-type]
* cpp23[meta cpp]

```cpp
namespace std {
template<semiregular S>
constexpr const_sentinel<S> make_const_sentinel(S s);
}
```
* semiregular[link semiregular.md]
* const_sentinel[link const_sentinel.md.nolink]
## 概要
`basic_const_iterator`のヘルパ関数。特に、イテレータではないような番兵を`basic_const_iterator`の番兵型へと変換するのに使用する。
## 戻り値
```cpp
return s;
```

戻り値型は必ずしも`basic_const_iterator`の特殊化になるわけではない。

##
```cpp example
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main() {
std::vector vec = {1, 2, 3, 4, 11, 5, 6, 22};

auto cit = std::make_const_iterator(rng.begin());
auto cse = std::make_const_sentinel(std::unreachable_sentinel); // unreachable_sentinelはイテレータではない汎用の番兵

auto pos = std::ranges::find_if(cit, cse, [](auto& n) {
// 述語中で誤って変更してしまうことを防止する
// n = 0;
return 10 <= n;
});

std::cout << *pos;
}
```
* std::make_const_sentinel[color ff0000]
* make_const_iterator[link make_const_iterator.md]
* unreachable_sentinel[link unreachable_sentinel_t.md]

### 出力
```
11
```

## バージョン
### 言語
- C++23

### 処理系
- [Clang](/implementation.md#clang): ??
- [GCC](/implementation.md#gcc): 13.1
- [Visual C++](/implementation.md#visual_cpp): 2022 Update 6

## 参照

- [P2278R4 `cbegin` should always return a constant iterator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2278r4.html)

1 comment on commit 396b2b6

@onihusube
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

間違い #1084

Please sign in to comment.