# std::optional<T>::end

> C++ Atlas: `cppatlas.com/w/cpp/utility/optional/end` | Source: https://en.cppreference.com/w/cpp/utility/optional/end | Domain: Utility | Standards: C++26

Returns a past-the-end iterator. Equivalent to return begin() + has_value();.

## Official Specification Summary (cppreference)

```cpp
constexpr iterator end() noexcept; // (since C++26)
constexpr const_iterator end() const noexcept; // (since C++26)
```

Returns a past-the-end iterator. Equivalent to `return begin() + has_value();`.

### Parameters

(none)

### Return value

Past-the-end iterator

### Complexity

Constant.

### Notes

| Feature-test macro | Value | Std | Feature |
| --- | --- | --- | --- |
| `__cpp_lib_optional_range_support` | `202406L` | (C++26) | Range support for `std::optional` |

### Example

```cpp
#include <optional>
#include <print>

int main()
{
    constexpr std::optional<int> none = std::nullopt; // optional @1
    constexpr std::optional<int> some = 42;           // optional @2

    static_assert(none.begin() == none.end());
    static_assert(some.begin() != some.end());

    // ranged-for loop support
    for (int i : none)
        std::println("Optional @1 has a value of {}", i);

    for (int i : some)
        std::println("Optional @2 has a value of {}", i);
}
```

Output:

```text
Optional @2 has a value of 42
```

### See also

- [`begin`](https://en.cppreference.com/w/cpp/utility/optional/begin) (C++26): returns an iterator to the beginning (public member function)

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