Skip to content

Commit

Permalink
P3168R2 Give std::optional Range Support
Browse files Browse the repository at this point in the history
  • Loading branch information
burblebee authored and tkoeppe committed Jul 12, 2024
1 parent 7c3f1a3 commit ebbe8c0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions source/support.tex
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@
#define @\defnlibxname{cpp_lib_not_fn}@ 202306L // freestanding, also in \libheader{functional}
#define @\defnlibxname{cpp_lib_null_iterators}@ 201304L // freestanding, also in \libheader{iterator}
#define @\defnlibxname{cpp_lib_optional}@ 202110L // also in \libheader{optional}
#define @\defnlibxname{cpp_lib_optional_range_support}@ 202406L // freestanding, also in \libheader{optional}
#define @\defnlibxname{cpp_lib_out_ptr}@ 202311L // freestanding, also in \libheader{memory}
#define @\defnlibxname{cpp_lib_parallel_algorithm}@ 201603L // also in \libheader{algorithm}, \libheader{numeric}
#define @\defnlibxname{cpp_lib_polymorphic_allocator}@ 201902L // also in \libheader{memory_resource}
Expand Down
68 changes: 67 additions & 1 deletion source/utilities.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3168,6 +3168,11 @@
template<class T>
class optional; // partially freestanding

template<class T>
constexpr bool ranges::enable_view<optional<T>> = true;
template<class T>
constexpr auto format_kind<optional<T>> = range_format::disabled;

template<class T>
concept @\defexposconcept{is-derived-from-optional}@ = requires(const T& t) { // \expos
[]<class U>(const optional<U>&){ }(t);
Expand Down Expand Up @@ -3248,7 +3253,9 @@
template<class T>
class optional {
public:
using value_type = T;
using value_type = T;
using iterator = @\impdefnc@; // see~\ref{optional.iterators}
using const_iterator = @\impdefnc@; // see~\ref{optional.iterators}

// \ref{optional.ctor}, constructors
constexpr optional() noexcept;
Expand Down Expand Up @@ -3282,6 +3289,12 @@
// \ref{optional.swap}, swap
constexpr void swap(optional&) noexcept(@\seebelow@);

// \ref{optional.iterators}, iterator support
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;

// \ref{optional.observe}, observers
constexpr const T* operator->() const noexcept;
constexpr T* operator->() noexcept;
Expand Down Expand Up @@ -4001,6 +4014,59 @@
the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s move constructor.
\end{itemdescr}

\rSec3[optional.iterators]{Iterator support}

\indexlibrarymember{iterator}{optional}%
\indexlibrarymember{const_iterator}{optional}%
\begin{itemdecl}
using iterator = @\impdef@;
using const_iterator = @\impdef@;
\end{itemdecl}

\begin{itemdescr}
\pnum
These types
model \libconcept{contiguous_iterator}\iref{iterator.concept.contiguous},
meet the \oldconcept{RandomAccessIterator} requirements\iref{random.access.iterators}, and
meet the requirements for constexpr iterators\iref{iterator.requirements.general},
with value type \tcode{remove_cv_t<T>}.
The reference type is \tcode{T\&} for \tcode{iterator} and
\tcode{const T\&} for \tcode{const_iterator}.

\pnum
All requirements on container iterators\iref{container.reqmts} apply to
\tcode{optional::iterator} and \tcode{optional::\linebreak{}const_iterator} as well.

\pnum
Any operation that initializes or destroys the contained value of an optional object invalidates all iterators into that object.
\end{itemdescr}

\indexlibrarymember{begin}{optional}%
\begin{itemdecl}
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
\end{itemdecl}

\begin{itemdescr}
\pnum
\returns
If \tcode{has_value()} is \tcode{true},
an iterator referring to the contained value.
Otherwise, a past-the-end iterator value.
\end{itemdescr}

\indexlibrarymember{end}{optional}%
\begin{itemdecl}
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
\end{itemdecl}

\begin{itemdescr}
\pnum
\returns
\tcode{begin() + has_value()}.
\end{itemdescr}

\rSec3[optional.observe]{Observers}

\indexlibrarymember{operator->}{optional}%
Expand Down

0 comments on commit ebbe8c0

Please sign in to comment.