namespace std::ranges {
// (1)
template<class I, class T>
struct in_value_result {
[[no_unique_address]] I in;
[[no_unique_address]] T value;
template<class I2, class T2>
requires convertible_to<const I&, I2> &&
convertible_to<const T&, T2>
constexpr operator in_value_result<I2, T2>() const & {
return {in, value};
}
template<class I2, class T2>
requires convertible_to<I, I2> &&
convertible_to<T, T2>
constexpr operator in_value_result<I2, T2>() && {
return {std::move(in), std::move(value)};
}
};
// (2)
template<class I, class T>
using fold_left_with_iter_result = in_value_result<I, T>;
// (3)
template<class I, class T>
using fold_left_first_with_iter_result = in_value_result<I, T>;
}
概要
- (1): イテレータと値を格納する型
- (2):
ranges::fold_left_with_iterで使用するエイリアス - (3):
ranges::fold_left_first_with_iterで使用するエイリアス
この型は、関数が入力用に範囲を受け取る場合に、処理した範囲の末尾と、それとは別の出力を返すために使用される。
標準アルゴリズム関数ではこの型を直接返す代わりに、関数毎にエイリアスを定義している。
メンバ変数
| 名前 | 説明 | 対応バージョン |
|---|---|---|
[[no_unique_address]] I in |
読み込んだ範囲の終端位置 | C++23 |
[[no_unique_address]] T value |
イテレータとは別の値 | C++23 |
メンバ関数
| 名前 | 説明 | 対応バージョン |
|---|---|---|
operator in_value_result<I2, T2> |
変換演算子 | C++23 |
変換演算子は、各テンプレートパラメーターが変換できる場合のみオーバーロード解決に参加する。
例
#include <cassert>
#include <algorithm>
#include <functional>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
// fold_left_with_iterはin_value_result(fold_left_with_iter_result)を返す
const std::ranges::in_value_result result = std::ranges::fold_left_with_iter(v, 0, std::plus<>{});
assert(result.in == v.end()); // 走査し終えた終端イテレータ
assert(result.value == 10); // 畳み込み結果
}
出力
バージョン
言語
- C++23
処理系
- Clang: ??
- GCC: 13.1 ✅
- Visual C++: 2022 Update 5 ✅