GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::optional::begin - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. optional
  3. optional
  4. begin

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<optional>

std::optional::begin(C++26)

constexpr iterator begin() noexcept; // (1)
constexpr const_iterator begin() const noexcept; // (2)

概要

optionalをrangeとした時の先頭要素を指すイテレータを取得する。

効果

has_value() == trueなら、保持している有効値を指すイテレータを返す。またこの時、distance(begin(), end()) == 1 となる。

has_value() != trueなら、末尾要素の次を指すイテレータを返す。この時、begin() == end() である。

例

#include <iostream>
#include <optional>

int main()
{
  std::optional<int> p1 = 1;
  for (auto i = p1.begin(); i != p1.end(); ++i) {
    std::cout << *i; // 1度通る
  }

  std::optional<int> p2 = std::nullopt;
  for (auto i = p2.begin(); i != p2.end(); ++i) {
    std::cout << *i; // 1度も通らない
  }
}

出力

1

バージョン

言語

  • C++26

処理系

  • Clang: ??
  • GCC: ??
  • Visual C++: ??

参照

  • P3168R2 Give std::optional Range Support
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.