Namespaces
Variants

Standard library header <optional> (C++17)

From cppreference.net
Standard library headers

이 헤더는 범용 유틸리티 라이브러리의 일부입니다.

목차

포함 파일

(C++20)
3-way 비교 연산자 지원

클래스

(C++17)
객체를 보유할 수도 있고 보유하지 않을 수도 있는 래퍼
(클래스 템플릿)
값을 포함하지 않는 optional에 대한 검사된 접근을 나타내는 예외
(클래스)
std::optional 에 대한 해시 지원
(클래스 템플릿 특수화)
(C++17)
값을 포함하지 않는 std::optional 의 표시자
(클래스)
전방 선언
헤더 파일 <functional> 에 정의됨
(C++11)
해시 함수 객체
(클래스 템플릿)

상수

(C++17)
nullopt_t 타입의 객체
(상수)

함수

비교 연산
(C++17) (C++17) (C++17) (C++17) (C++17) (C++17) (C++20)
optional 객체 비교
(함수 템플릿)
특수화된 알고리즘
std::swap 알고리즘 특수화
(함수 템플릿)
optional 객체 생성
(함수 템플릿)

시놉시스

// 대부분 독립 실행형
#include <compare>
namespace std {
  // 클래스 템플릿 optional
  template<class T> class optional;                             // 부분적으로 독립형
  template<class T> constexpr bool ranges::enable_view<optional<T>> = true;
  template<class T> constexpr auto format_kind<optional<T>> = range_format::비활성화됨;
  template<class T>
  concept /*is-derived-from-optional*/ = requires(const T& t) { // exposition-only
    []<class U>(const optional<U>&) {}(t);
  };
  // 값 없음 상태 표시기
  struct nullopt_t
  { /* 설명 참조 */
  };
  inline constexpr nullopt_t nullopt(/* 미지정 */);
  // class bad_optional_access
  class bad_optional_access;
  // 관계 연산자
  template<class T, class U>
  constexpr bool operator==(const optional<T>&, const optional<U>&);
  template<class T, class U>
  constexpr bool operator!=(const optional<T>&, const optional<U>&);
  template<class T, class U>
  constexpr bool operator<(const optional<T>&, const optional<U>&);
  template<class T, class U>
  constexpr bool operator>(const optional<T>&, const optional<U>&);
  template<class T, class U>
  constexpr bool operator<=(const optional<T>&, const optional<U>&);
  template<class T, class U>
  constexpr bool operator>=(const optional<T>&, const optional<U>&);
  template<class T, three_way_comparable_with<T> U>
  constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>&,
                                                         const optional<U>&);
  // nullopt와의 비교
  template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept;
  template<class T>
  constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept;
  // T와의 비교
  template<class T, class U> constexpr bool operator==(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator==(const T&, const optional<U>&);
  template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&);
  template<class T, class U> constexpr bool operator<(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator<(const T&, const optional<U>&);
  template<class T, class U> constexpr bool operator>(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator>(const T&, const optional<U>&);
  template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&);
  template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&);
  template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&);
  template<class T, class U>
    requires(!/*is-derived-from-optional*/<U>) && three_way_comparable_with<T, U>
  constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>&, const U&);
  // 특수화된 알고리즘
  template<class T>
  constexpr void swap(optional<T>&, optional<T>&) noexcept(/* 설명 참조 */);
  template<class T> constexpr optional<decay_t<T>> make_optional(T&&);
  template<class T, class... Args> constexpr optional<T> make_optional(Args&&... args);
  template<class T, class U, class... Args>
  constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
  // 해시 지원
  template<class T> struct hash;
  template<class T> struct hash<optional<T>>;
}

클래스 템플릿 std::optional

namespace std {
  template<class T> class optional
  {
  public:
    using value_type = T;
    using iterator = /* 구현 정의 */;
    using const_iterator = /* 구현 정의 */;
    // 생성자
    constexpr optional() noexcept;
    constexpr optional(nullopt_t) noexcept;
    constexpr optional(const optional&);
    constexpr optional(optional&&) noexcept(/* 설명 참조 */);
    template<class... Args> constexpr explicit optional(in_place_t, Args&&...);
    template<class U, class... Args>
    constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
    template<class U = remove_cv_t<T>>
    constexpr explicit(/* 설명 참조 */) optional(U&&);
    template<class U>
    constexpr explicit(/* 설명 참조 */) optional(const optional<U>&);
    template<class U> constexpr explicit(/* 설명 참조 */) optional(optional<U>&&);
    // 소멸자
    constexpr ~optional();
    // assignment
    constexpr optional& operator=(nullopt_t) noexcept;
    constexpr optional& operator=(const optional&);
    constexpr optional& operator=(optional&&) noexcept(/* 설명 참조 */);
    template<class U = remove_cv_t<T>> constexpr optional& operator=(U&&);
    template<class U> constexpr optional& operator=(const optional<U>&);
    template<class U> constexpr optional& operator=(optional<U>&&);
    template<class... Args> constexpr T& emplace(Args&&...);
    template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);
    // swap
    constexpr void swap(optional&) noexcept(/* 설명 참조 */);
    // iterator support
    constexpr iterator begin() noexcept;
    constexpr const_iterator begin() const noexcept;
    constexpr iterator end() noexcept;
    constexpr const_iterator end() const noexcept;
    // observers
    constexpr const T* operator->() const noexcept;
    constexpr T* operator->() noexcept;
    constexpr const T& operator*() const& noexcept;
    constexpr T& operator*() & noexcept;
    constexpr T&& operator*() && noexcept;
    constexpr const T&& operator*() const&& noexcept;
    constexpr explicit operator bool() const noexcept;
    constexpr bool has_value() const noexcept;
    constexpr const T& value() const&;   // freestanding-deleted
    constexpr T& value() &;              // freestanding-deleted
    constexpr T&& value() &&;            // freestanding-deleted
    constexpr const T&& value() const&&; // freestanding-deleted
    template<class U = remove_cv_t<T>> constexpr T value_or(U&&) const&;
    template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&;
    // 모나딕 연산
    template<class F> constexpr auto and_then(F&& f) &;
    template<class F> constexpr auto and_then(F&& f) &&;
    template<class F> constexpr auto and_then(F&& f) const&;
    template<class F> constexpr auto and_then(F&& f) const&&;
    template<class F> constexpr auto transform(F&& f) &;
    template<class F> constexpr auto transform(F&& f) &&;
    template<class F> constexpr auto transform(F&& f) const&;
    template<class F> constexpr auto transform(F&& f) const&&;
    template<class F> constexpr optional or_else(F&& f) &&;
    template<class F> constexpr optional or_else(F&& f) const&;
    // modifiers
    constexpr void reset() noexcept;
  private:
    T* val; // exposition-only
  };
  template<class T> optional(T) -> optional<T>;
}

클래스 템플릿 std::bad_optional_access

namespace std {
  class bad_optional_access : public exception
  {
  public:
    // 특수 멤버 함수의 명세를 위해
    constexpr const char* what() const noexcept override;
  };
}