std::ranges::subrange<I,S,K>::operator PairLike - cppreference.com (original) (raw)
| template< /*different-from*/<subrange> PairLike > requires /*pair-like-convertible-from*/<PairLike, const I&, const S&> constexpr operator PairLike() const; | (1) | (since C++20) |
|---|---|---|
| Helper concepts | ||
| template< class T >concept /*pair-like*/ = /* see description */; | (2) | (exposition only*) |
| template< class T, class U, class V >concept /*pair-like-convertible-from*/ = /* see description */; | (3) | (exposition only*) |
- Converts
subrangeto a pair-like type.
For the definition of /*different-from*/, see _[different-from](../../ranges.html#different-from "cpp/ranges")_ .
Determines whether a type is pair-like .
Determines whether a pair-like type can be constructed from two values of possibly different given types.
| Equivalent to: | (until C++23) |
|---|---|
| Equivalent to: | (since C++23) |
[edit] Return value
PairLike(_[begin](../subrange.html#begin "cpp/ranges/subrange")_ , _[end](../subrange.html#end "cpp/ranges/subrange")_ )
[edit] Notes
Following types in the standard library are pair-like:
- std::pair<T, U>
- std::tuple<T, U>
- std::array<T, 2>
- std::ranges::subrange<I, S, K>
| A program-defined type derived from one of these types can be a pair-like type, if std::tuple_size and std::tuple_element are correctly specialized for it, and calls to std::get<0> and std::get<1> for its value are well-formed. | (until C++23) |
|---|
Since subrange specializations are range types, conversion to them are not performed via this conversion function.
std::array specializations cannot be converted from subrange, since they are range types.
[edit] Example
#include #include #include #include using striter = std:🧵:const_iterator; using legacy_strview = std::pair<striter, striter>; void legacy_print(legacy_strview p) { for (; p.first != p.second; ++p.first) std::cout << p.first << ' '; std::cout << '\n'; } int main() { std::string dat{"ABCDE"}; for (auto v{std::ranges::subrange{dat}}; v; v = {v.begin(), v.end() - 1}) { /...*/ legacy_print(legacy_strview{v}); } }
Output:
A B C D E A B C D A B C A B A