std::ranges::get(std::ranges::subrange)

Header: <ranges>

Provides structured binding support.

# Declarations

template< std::size_t N, class I, class S, ranges::subrange_kind K >
requires ((N == 0 && std::copyable<I>) || N == 1)
constexpr auto get( const ranges::subrange<I, S, K>& r );

(since C++20)

template< std::size_t N, class I, class S, ranges::subrange_kind K >
requires (N < 2)
constexpr auto get( ranges::subrange<I, S, K>&& r );

(since C++20)

namespace std { using ranges::get; }

(since C++20)

# Parameters

# Example

#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)
              << *std::ranges::get<1>(sub_a) << '\n'; // == *(end(a) - 1)
 
    *std::get<0>(sub_a) = 42; // OK
//  *std::get<2>(sub_a) = 13; // Error: index can only be 0 or 1
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 3589C++20overload (1) copies begin_ if N is 0, but I might not model copyableadded constraints

# See also