# std::tuple_size<std:ranges::subrange>

> C++ Atlas: `cppatlas.com/w/cpp/ranges/subrange/tuple_size` | Source: https://en.cppreference.com/w/cpp/ranges/subrange/tuple_size | Domain: Ranges | Standards: C++11, C++14, C++17, C++20

The partial specialization of std::tuple_size for std::ranges::subrange provides a compile-time way to obtain the number of components of a subrange, which is always 2, using tuple-like syntax.

## Official Specification Summary (cppreference)

```cpp
// Defined in header <ranges>

template< class I, class S, ranges::subrange_kind K >
struct tuple_size<ranges::subrange<I, S, K>>
    : std::integral_constant<std::size_t, 2> {}; // (since C++20)
```

The partial specialization of `std::tuple_size` for `std::ranges::subrange` provides a compile-time way to obtain the number of components of a `subrange`, which is always 2, using tuple-like syntax. It is provided for structured binding support.

### Inherited from std::integral_constant

#### Member constants

- `value` [static]: the constant value 2 (public static member constant)

#### Member functions

- `operator std::size_t`: converts the object to `std::size_t`, returns `value` (public member function)
- `operator()` (C++14): returns `value` (public member function)

#### Member types

**Type — Definition**

- `value_type`: `std::size_t`
- `type`: `std::integral_constant<std::size_t, value>`

#### Example

```cpp
#include <array>
#include <iostream>
#include <iterator>
#include <ranges>

int main()
{
    static_assert(2 == std::tuple_size_v<std::ranges::subrange<int*, int*>>);

    using array5 = std::array<int, 5>;

    static_assert(2 == std::tuple_size<std::ranges::subrange<
        array5::const_iterator, array5::const_iterator>>{});

    constexpr array5 a{1, 2, 3, 4, 5};

    std::ranges::subrange sub_a1{a};

    for (std::cout << "sub_a1: { "; int e : sub_a1)
        std::cout << e << ' ';
    std::cout << "}\n";

    std::ranges::subrange sub_a2{std::next(cbegin(a)), std::prev(cend(a))};

    const auto [first, last] = sub_a2;
    std::cout << "sub_a2 size = " << std::distance(first, last) << '\n';

    for (std::cout << "sub_a2: { "; int e : sub_a2)
        std::cout << e << ' ';
    std::cout << "}\n";
}
```

Output:

```text
sub_a1: { 1 2 3 4 5 }
sub_a2 size = 3
sub_a2: { 2 3 4 }
```

#### See also

- [Structured binding (C++17)](https://en.cppreference.com/w/cpp/language/structured_binding): binds the specified names to sub-objects or tuple elements of the initializer
- [`tuple_size`](https://en.cppreference.com/w/cpp/utility/tuple_size) (C++11): obtains the number of elements of a tuple-like type (class template)
- [`std::tuple_size<std::tuple>`](https://en.cppreference.com/w/cpp/utility/tuple/tuple_size) (C++11): obtains the size of a `tuple` (class template specialization)
- [`std::tuple_size<std::pair>`](https://en.cppreference.com/w/cpp/utility/pair/tuple_size) (C++11): obtains the size of a `pair` (class template specialization)
- [`std::tuple_size<std::array>`](https://en.cppreference.com/w/cpp/container/array/tuple_size) (C++11): obtains the size of an `array` (class template specialization)
- [`std::tuple_element<std::ranges::subrange>`](https://en.cppreference.com/w/cpp/ranges/subrange/tuple_element) (C++20): obtains the type of the iterator or the sentinel of a `std::ranges::subrange` (class template specialization)

---
Attribution: content from https://en.cppreference.com/w/cpp/ranges/subrange/tuple_size (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.