# std::ranges::borrowed_iterator_t, std::ranges::borrowed_subrange_t

> C++ Atlas: `cppatlas.com/w/cpp/ranges/borrowed_iterator_t` | Source: https://en.cppreference.com/w/cpp/ranges/borrowed_iterator_t | Domain: Ranges | Standards: C++20

These two alias templates are used by some constrained algorithms to avoid returning potentially dangling iterators or views.

## Official Specification Summary (cppreference)

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

template< ranges::range R >
using borrowed_iterator_t = /* see below */; // (1) (since C++20)

template< ranges::range R >
using borrowed_subrange_t = /* see below */; // (2) (since C++20)
```

1) `std::ranges::iterator_t<R>` if `R` models `borrowed_range`, `std::ranges::dangling` otherwise.

2) `std::ranges::subrange<std::ranges::iterator_t<R>>` if `R` models `borrowed_range`, `std::ranges::dangling` otherwise.

These two alias templates are used by some constrained algorithms to avoid returning potentially dangling iterators or views.

### Possible implementation

```cpp
template< std::ranges::range R >
using borrowed_iterator_t = std::conditional_t<std::ranges::borrowed_range<R>,
    std::ranges::iterator_t<R>, std::ranges::dangling>;
```

**borrowed_subrange_t**

```cpp
template< std::ranges::range R >
using borrowed_subrange_t = std::conditional_t<std::ranges::borrowed_range<R>,
    std::ranges::subrange<std::ranges::iterator_t<R>>, std::ranges::dangling>;
```

### See also

- [`ranges::dangling`](https://en.cppreference.com/w/cpp/ranges/dangling) (C++20): a placeholder type indicating that an iterator or a `subrange` should not be returned since it would be dangling (class)

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