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::operator bool - cpprefjp C++日本語リファレンス

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

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

履歴 編集

function
<optional>

std::optional::operator bool(C++17)

constexpr explicit operator bool() const noexcept;

概要

有効な値を保持しているかを判定する。

効果

有効値を保持している場合にtrueを返し、そうでなければfalseを返す。

例

#include <iostream>
#include <optional>

int main()
{
  std::optional<int> p1 = 3;
  if (p1) {
    std::cout << "p1 has value" << std::endl;
  }

  std::optional<int> p2;
  if (!p2) {
    std::cout << "p2 doesn't have value" << std::endl;
  }
}

出力

p1 has value
p2 doesn't have value

バージョン

言語

  • C++17

処理系

  • Clang: 4.0.1 ✅
  • GCC: 7.2 ✅
  • Visual C++: ??

関連項目

  • optional::has_value()
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.