Skip to content

Commit ebbe8c0

Browse files
burblebeetkoeppe
authored andcommitted
P3168R2 Give std::optional Range Support
1 parent 7c3f1a3 commit ebbe8c0

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

source/support.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@
726726
#define @\defnlibxname{cpp_lib_not_fn}@ 202306L // freestanding, also in \libheader{functional}
727727
#define @\defnlibxname{cpp_lib_null_iterators}@ 201304L // freestanding, also in \libheader{iterator}
728728
#define @\defnlibxname{cpp_lib_optional}@ 202110L // also in \libheader{optional}
729+
#define @\defnlibxname{cpp_lib_optional_range_support}@ 202406L // freestanding, also in \libheader{optional}
729730
#define @\defnlibxname{cpp_lib_out_ptr}@ 202311L // freestanding, also in \libheader{memory}
730731
#define @\defnlibxname{cpp_lib_parallel_algorithm}@ 201603L // also in \libheader{algorithm}, \libheader{numeric}
731732
#define @\defnlibxname{cpp_lib_polymorphic_allocator}@ 201902L // also in \libheader{memory_resource}

source/utilities.tex

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3168,6 +3168,11 @@
31683168
template<class T>
31693169
class optional; // partially freestanding
31703170

3171+
template<class T>
3172+
constexpr bool ranges::enable_view<optional<T>> = true;
3173+
template<class T>
3174+
constexpr auto format_kind<optional<T>> = range_format::disabled;
3175+
31713176
template<class T>
31723177
concept @\defexposconcept{is-derived-from-optional}@ = requires(const T& t) { // \expos
31733178
[]<class U>(const optional<U>&){ }(t);
@@ -3248,7 +3253,9 @@
32483253
template<class T>
32493254
class optional {
32503255
public:
3251-
using value_type = T;
3256+
using value_type = T;
3257+
using iterator = @\impdefnc@; // see~\ref{optional.iterators}
3258+
using const_iterator = @\impdefnc@; // see~\ref{optional.iterators}
32523259

32533260
// \ref{optional.ctor}, constructors
32543261
constexpr optional() noexcept;
@@ -3282,6 +3289,12 @@
32823289
// \ref{optional.swap}, swap
32833290
constexpr void swap(optional&) noexcept(@\seebelow@);
32843291

3292+
// \ref{optional.iterators}, iterator support
3293+
constexpr iterator begin() noexcept;
3294+
constexpr const_iterator begin() const noexcept;
3295+
constexpr iterator end() noexcept;
3296+
constexpr const_iterator end() const noexcept;
3297+
32853298
// \ref{optional.observe}, observers
32863299
constexpr const T* operator->() const noexcept;
32873300
constexpr T* operator->() noexcept;
@@ -4001,6 +4014,59 @@
40014014
the state of \tcode{*val} and \tcode{*rhs.val} is determined by the exception safety guarantee of \tcode{T}'s move constructor.
40024015
\end{itemdescr}
40034016

4017+
\rSec3[optional.iterators]{Iterator support}
4018+
4019+
\indexlibrarymember{iterator}{optional}%
4020+
\indexlibrarymember{const_iterator}{optional}%
4021+
\begin{itemdecl}
4022+
using iterator = @\impdef@;
4023+
using const_iterator = @\impdef@;
4024+
\end{itemdecl}
4025+
4026+
\begin{itemdescr}
4027+
\pnum
4028+
These types
4029+
model \libconcept{contiguous_iterator}\iref{iterator.concept.contiguous},
4030+
meet the \oldconcept{RandomAccessIterator} requirements\iref{random.access.iterators}, and
4031+
meet the requirements for constexpr iterators\iref{iterator.requirements.general},
4032+
with value type \tcode{remove_cv_t<T>}.
4033+
The reference type is \tcode{T\&} for \tcode{iterator} and
4034+
\tcode{const T\&} for \tcode{const_iterator}.
4035+
4036+
\pnum
4037+
All requirements on container iterators\iref{container.reqmts} apply to
4038+
\tcode{optional::iterator} and \tcode{optional::\linebreak{}const_iterator} as well.
4039+
4040+
\pnum
4041+
Any operation that initializes or destroys the contained value of an optional object invalidates all iterators into that object.
4042+
\end{itemdescr}
4043+
4044+
\indexlibrarymember{begin}{optional}%
4045+
\begin{itemdecl}
4046+
constexpr iterator begin() noexcept;
4047+
constexpr const_iterator begin() const noexcept;
4048+
\end{itemdecl}
4049+
4050+
\begin{itemdescr}
4051+
\pnum
4052+
\returns
4053+
If \tcode{has_value()} is \tcode{true},
4054+
an iterator referring to the contained value.
4055+
Otherwise, a past-the-end iterator value.
4056+
\end{itemdescr}
4057+
4058+
\indexlibrarymember{end}{optional}%
4059+
\begin{itemdecl}
4060+
constexpr iterator end() noexcept;
4061+
constexpr const_iterator end() const noexcept;
4062+
\end{itemdecl}
4063+
4064+
\begin{itemdescr}
4065+
\pnum
4066+
\returns
4067+
\tcode{begin() + has_value()}.
4068+
\end{itemdescr}
4069+
40044070
\rSec3[optional.observe]{Observers}
40054071

40064072
\indexlibrarymember{operator->}{optional}%

0 commit comments

Comments
 (0)