std::ranges...s::subrange)(3)
π NAME π
std::ranges::get(std::ranges::subrange) β std::ranges::get(std::ranges::subrange). Bestie, it's the get overload set that pulls the iterator or sentinel out of a std::ranges::subrange. That's the whole name. No cap. π«Ά
This is from the C++ Standard Libary manual β yes, that's the manual name, we're folding it in right here at the top instead of copying the header line. π
β¨ Synopsis β¨
Defined in header <ranges>
template< std::size_t N, class I, class S, ranges::subrange_kind K
>
(1)
(since C++20)
requires ((N == 0 && std::copyable<I>) || N == 1)
constexpr auto get( const ranges::subrange<I, S, K>& r );
template< std::size_t N, class I, class S, ranges::subrange_kind K
>
(2)
(since C++20)
requires (N < 2)
constexpr auto get( ranges::subrange<I, S, K>&& r );
namespace std { using ranges::get; }
(3) (since C++20)
- Obtains the iterator or sentinel from a subrange lvalue (or a const rvalue) when
N == 0orN == 1, respectively. It is mainly provided for structured binding support. Periodt. π - Same as (1), except that it takes a non-const subrange rvalue. Subtle, but iconic. β¨
- (1,2) are imported into namespace
std, which simplifies their usage and makes every subrange with a copyable iterator a pair-like type. iykyk. π
π Parameters π
rβ a subrange. That's it. That's the parameter list, bestie. π
πΈ Return value πΈ
- An iterator or sentinel copy constructed from the stored one when
N == 0orN == 1, respectively. copy constructed, not invented, okay? π«Ά - Same as (1), except that the iterator is move constructed if
N == 0andIdoes not satisfycopyable. Move construction said "let me handle this." π¦
π οΈ Possible implementation π οΈ
template<std::size_t N, class I, class S, std::ranges::subrange_kind
K>
requires ((N == 0 && std::copyable<I>) || N == 1)
constexpr auto get(const std::ranges::subrange<I, S, K>& r)
{
if constexpr (N == 0)
return r.begin();
else
return r.end();
}
template<std::size_t N, class I, class S, std::ranges::subrange_kind
K>
requires (N < 2)
constexpr auto get(std::ranges::subrange<I, S, K>&& r)
{
if constexpr (N == 0)
return r.begin(); // may perform move construction
else
return r.end();
}
π Example π
// Run this code β yes bestie, go run it. πββοΈπ¨
#include <array>
#include <iostream>
#include <iterator>
#include <ranges>
int main()
{
std::array a{1, -2, 3, -4};
std::ranges::subrange sub_a{std::next(a.begin()),
std::prev(a.end())};
std::cout << *std::ranges::get<0>(sub_a) << ' ' // == *(begin(a) +
1)
<< *get<1>(sub_a) << '\n'; // == *(end(a) - 1)
*get<0>(sub_a) = 42; // OK
// *get<2>(sub_a) = 13; // hard error: index can only be 0 or 1
}
π» Output π»
-2 -4
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History really said "let me fix that." π
- LWG 3589 β applied to C++20. Behavior as published: the overload for const lvalue was ill-formed if
N == 0andIdoes not modelcopyable. Correct behavior: it is removed from the overload set. Slay. π
π¦ See also π¦
- Structured binding (C++17) binds the specified names to sub-objects or tuple elements of the initializer. the moment structured bindings ate. π
get(std::tuple)(C++11) tuple accesses specified element (function template). β¨get(std::pair)(C++11) accesses an element of a pair (function template). πget(std::array)(C++11) accesses an element of an array (function template). πΈ
okay that's the whole page, bestie π
β source/version/date: http://cppreference.com, 2024.06.10, and the manual is the C++ Standard Libary one. mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms