Namespaces
Variants

std::optional<T>:: end

From cppreference.net
Utilities library
constexpr iterator end ( ) noexcept ;
(C++26以降)
constexpr const_iterator end ( ) const noexcept ;
(C++26以降)

終端イテレータを返します。次と等価です: return begin ( ) + has_value ( ) ;

range-begin-end.svg

目次

戻り値

末尾イテレータの次

計算量

定数。

注記

機能テスト マクロ 標準 機能
__cpp_lib_optional_range_support 202406L (C++26) std::optional の範囲サポート

#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());
    // 範囲ベースforループのサポート
    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);
}

出力:

Optional @2 has a value of 42

関連項目

(C++26)
先頭を指すイテレータを返す
(公開メンバ関数)