From 8e89dcc41392bb9f4a6a9a3ea857f469e7a4c535 Mon Sep 17 00:00:00 2001 From: William Tran-Viet Date: Sun, 30 Nov 2025 13:06:53 -0500 Subject: [PATCH] Add optional synopsis --- libcxx/include/optional | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/libcxx/include/optional b/libcxx/include/optional index 23b21364b1a79..7b979d3d6d577 100644 --- a/libcxx/include/optional +++ b/libcxx/include/optional @@ -186,6 +186,71 @@ namespace std { template optional(T) -> optional; + template + class optional { // since C++26 + public: + using value_type = T; + using iterator = implementation-defined; // see [optional.ref.iterators] + + public: + // [optional.ref.ctor], constructors + constexpr optional() noexcept = default; + constexpr optional(nullopt_t) noexcept : optional() {} + constexpr optional(const optional& rhs) noexcept = default; + + template + constexpr explicit optional(in_place_t, Arg&& arg); + template + constexpr explicit(see below) optional(U&& u) noexcept(see below); + template + constexpr explicit(see below) optional(optional& rhs) noexcept(see below); + template + constexpr explicit(see below) optional(const optional& rhs) noexcept(see below); + template + constexpr explicit(see below) optional(optional&& rhs) noexcept(see below); + template + constexpr explicit(see below) optional(const optional&& rhs) noexcept(see below); + + constexpr ~optional() = default; + + // [optional.ref.assign], assignment + constexpr optional& operator=(nullopt_t) noexcept; + constexpr optional& operator=(const optional& rhs) noexcept = default; + + template constexpr T& emplace(U&& u) noexcept(see below); + + // [optional.ref.swap], swap + constexpr void swap(optional& rhs) noexcept; + + // [optional.ref.iterators], iterator support + constexpr iterator begin() const noexcept; + constexpr iterator end() const noexcept; + + // [optional.ref.observe], observers + constexpr T* operator->() const noexcept; + constexpr T& operator*() const noexcept; + constexpr explicit operator bool() const noexcept; + constexpr bool has_value() const noexcept; + constexpr T& value() const; // freestanding-deleted + template> + constexpr remove_cv_t value_or(U&& u) const; + + // [optional.ref.monadic], monadic operations + template constexpr auto and_then(F&& f) const; + template constexpr optional> transform(F&& f) const; + template constexpr optional or_else(F&& f) const; + + // [optional.ref.mod], modifiers + constexpr void reset() noexcept; + + private: + T* val = nullptr; // exposition only + + // [optional.ref.expos], exposition only helper functions + template + constexpr void convert-ref-init-val(U&& u); // exposition only + }; + } // namespace std */