Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
constant_rangeとpossibly-const-range追加 #1084
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# constant_range | ||
* ranges[meta header] | ||
* concept[meta id-type] | ||
* std::ranges[meta namespace] | ||
* cpp23[meta cpp] | ||
|
||
```cpp | ||
namespace std::ranges { | ||
template<class T> | ||
concept constant_range = input_range<T> && constant-iterator<iterator_t<T>>; | ||
} | ||
``` | ||
* input_range[link input_range.md] | ||
* constant-iterator[link /reference/iterator/constant-iterator.md] | ||
* iterator_t[link iterator_t.md] | ||
## 概要 | ||
`constant_range`は、その要素が変更不可能な`range`を表すコンセプトである。 | ||
## 例 | ||
```cpp example | ||
#include <ranges> | ||
#include <vector> | ||
int main() { | ||
static_assert(not std::ranges::constant_range< std::vector<int>>); | ||
static_assert( std::ranges::constant_range<const std::vector<int>>); | ||
static_assert(std::ranges::constant_range<std::ranges::iota_view<int>>); | ||
} | ||
``` | ||
|
||
### 出力 | ||
|
||
``` | ||
``` | ||
|
||
## バージョン | ||
### 言語 | ||
- 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# possibly-const-range | ||
* ranges[meta header] | ||
* std::ranges[meta namespace] | ||
* function[meta id-type] | ||
* cpp23[meta cpp] | ||
|
||
```cpp | ||
namespace std::ranges { | ||
template<input_range R> | ||
constexpr auto& possibly-const-range(R& r) { | ||
if constexpr (constant_range<const R> && !constant_range<R>) { | ||
return const_cast<const R&>(r); | ||
} else { | ||
return r; | ||
} | ||
} | ||
} | ||
``` | ||
* input_range[link /reference/ranges/input_range.md] | ||
* constant_range[link /reference/ranges/constant_range.md] | ||
## 概要 | ||
`possibly-const-range`は入力の`input_range`オブジェクト`r`を、可能なら要素が定数化された`range`へ変換するものである。 | ||
主に、`ranges::cbegin()`など定数イテレータを取得したい場所で、イテレータ取得前の段階で`range`型の簡単な変換のみで定数イテレータを取得できるかを試行するのに使用される。 | ||
## バージョン | ||
### 言語 | ||
- C++23 | ||
## 参照 | ||
- [P2278R4 `cbegin` should always return a constant iterator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2278r4.html) |