Skip to content

P1252R2 Ranges Design Cleanup #2720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
508 changes: 292 additions & 216 deletions source/algorithms.tex

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions source/future.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,32 @@
\end{codeblock}
\end{example}

\rSec1[depr.move.iter.elem]{Deprecated \tcode{move_iterator} access}

\pnum
The following member is declared in addition to those members
specified in \ref{move.iter.elem}:

\begin{codeblock}
namespace std {
template<class Iterator>
class move_iterator {
public:
constexpr pointer operator->() const;
};
}
\end{codeblock}

\indexlibrarymember{operator->}{move_iterator}%
\begin{itemdecl}
constexpr pointer operator->() const;
\end{itemdecl}

\begin{itemdescr}
\pnum
\returns \tcode{current}.
\end{itemdescr}

\rSec1[depr.util.smartptr.shared.atomic]{Deprecated \tcode{shared_ptr} atomic access}

\pnum
Expand Down
21 changes: 5 additions & 16 deletions source/iterators.tex
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@

// \ref{alg.req.mergeable}, concept \tcode{Mergeable}
template<class I1, class I2, class Out,
class R = ranges::less<>, class P1 = identity, class P2 = identity>
class R = ranges::less, class P1 = identity, class P2 = identity>
concept Mergeable = @\seebelow@;

// \ref{alg.req.sortable}, concept \tcode{Sortable}
template<class I, class R = ranges::less<>, class P = identity>
template<class I, class R = ranges::less, class P = identity>
concept Sortable = @\seebelow@;

// \ref{iterator.primitives}, primitives
Expand Down Expand Up @@ -2277,7 +2277,7 @@

\pnum
\begin{note}
The \tcode{ranges::less<>} function object type
The \tcode{ranges::less} function object type
used in the concepts below imposes constraints on the concepts' arguments
in addition to those that appear in the concepts' bodies\iref{range.cmp}.
\end{note}
Expand Down Expand Up @@ -2428,7 +2428,7 @@

\indexlibrary{\idxcode{Mergeable}}%
\begin{codeblock}
template<class I1, class I2, class Out, class R = ranges::less<>,
template<class I1, class I2, class Out, class R = ranges::less,
class P1 = identity, class P2 = identity>
concept Mergeable =
InputIterator<I1> &&
Expand All @@ -2447,7 +2447,7 @@

\indexlibrary{\idxcode{Sortable}}%
\begin{codeblock}
template<class I, class R = ranges::less<>, class P = identity>
template<class I, class R = ranges::less, class P = identity>
concept Sortable =
Permutable<I> &&
IndirectStrictWeakOrder<R, projected<I, P>>;
Expand Down Expand Up @@ -3909,7 +3909,6 @@

constexpr iterator_type base() const;
constexpr reference operator*() const;
constexpr pointer operator->() const;

constexpr move_iterator& operator++();
constexpr auto operator++(int);
Expand Down Expand Up @@ -4062,16 +4061,6 @@
\effects Equivalent to: \tcode{return ranges::iter_move(current);}
\end{itemdescr}

\indexlibrarymember{operator->}{move_iterator}%
\begin{itemdecl}
constexpr pointer operator->() const;
\end{itemdecl}

\begin{itemdescr}
\pnum
\returns \tcode{current}.
\end{itemdescr}

\indexlibrarymember{operator[]}{move_iterator}%
\begin{itemdecl}
constexpr reference operator[](difference_type n) const;
Expand Down
2 changes: 1 addition & 1 deletion source/lib-intro.tex
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
\begin{example}
\begin{codeblock}
std::pair<int, std::string_view> pairs[] = {{2, "foo"}, {1, "bar"}, {0, "baz"}};
std::ranges::sort(pairs, std::ranges::less<>{}, [](auto const& p) { return p.first; });
std::ranges::sort(pairs, std::ranges::less{}, [](auto const& p) { return p.first; });
\end{codeblock}
sorts the pairs in increasing order of their \tcode{first} members:
\begin{codeblock}
Expand Down
123 changes: 100 additions & 23 deletions source/ranges.tex
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
template<class T>
using sentinel_t = decltype(ranges::end(declval<T&>()));

template<@\placeholder{forwarding-range}@ R>
using safe_iterator_t = iterator_t<R>;

template<class T>
concept Range = @\seebelow@;

Expand Down Expand Up @@ -112,8 +109,15 @@
requires (K == subrange_kind::sized || !SizedSentinel<S, I>)
class subrange;

template<@\placeholder{forwarding-range}@ R>
using safe_subrange_t = subrange<iterator_t<R>>;
// \ref{range.dangling}, dangling iterator handling
struct dangling;

template<Range R>
using safe_iterator_t = conditional_t<@\placeholder{forwarding-range}@<R>, iterator_t<R>, dangling>;

template<Range R>
using safe_subrange_t =
conditional_t<@\placeholder{forwarding-range}@<R>, subrange<iterator_t<R>>, dangling>;

// \ref{range.empty}, empty view
template<class T>
Expand Down Expand Up @@ -145,6 +149,10 @@
template<ViewableRange R>
using all_view = decltype(view::all(declval<R>()));

template<Range R>
requires is_object_v<R>
class ref_view;

// \ref{range.filter}, filter view
template<InputRange V, IndirectUnaryPredicate<iterator_t<V>> Pred>
requires View<V> && is_object_v<Pred>
Expand Down Expand Up @@ -180,7 +188,7 @@

template<InputRange V, ForwardRange Pattern>
requires View<V> && View<Pattern> &&
IndirectlyComparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to<>> &&
IndirectlyComparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
(ForwardRange<V> || @\placeholder{tiny-range}@<Pattern>)
class split_view;

Expand Down Expand Up @@ -1440,6 +1448,48 @@
\end{codeblock}
\end{itemdescr}

\rSec2[range.dangling]{Dangling iterator handling}

\pnum
The tag type \tcode{dangling} is used together
with the template aliases \tcode{safe_iterator_t} and \tcode{safe_subrange_t}
to indicate that an algorithm
that typically returns an iterator into or subrange of a \tcode{Range} argument
does not return an iterator or subrange
which could potentially reference a range
whose lifetime has ended for a particular rvalue \tcode{Range} argument
which does not model \tcode{\placeholder{forwarding-range}}\iref{range.range}.
\begin{codeblock}
namespace std {
struct dangling {
constexpr dangling() noexcept = default;
template<class... Args>
constexpr dangling(Args&&...) noexcept { }
};
}
\end{codeblock}

\pnum
\begin{example}
\begin{codeblock}
vector<int> f();
auto result1 = ranges::find(f(), 42); // \#1
static_assert(Same<decltype(result1), dangling>);
auto vec = f();
auto result2 = ranges::find(vec, 42); // \#2
static_assert(Same<decltype(result2), vector<int>::iterator>);
auto result3 = ranges::find(subrange{vec}, 42); // \#3
static_assert(Same<decltype(result3), vector<int>::iterator>);
\end{codeblock}
The call to \tcode{ranges::find} at \#1 returns \tcode{dangling}
since \tcode{f()} is an rvalue \tcode{vector};
the \tcode{vector} could potentially be destroyed
before a returned iterator is dereferenced.
However, the calls at \#2 and \#3 both return iterators
since the lvalue \tcode{vec} and specializations of \tcode{subrange}
model \tcode{\placeholder{forwarding-range}}.
\end{example}

\rSec1[range.factories]{Range factories}

\pnum
Expand Down Expand Up @@ -2347,28 +2397,28 @@
\item \tcode{\placeholdernc{decay-copy}(E)} if the decayed type of \tcode{E}
models \libconcept{View}.

\item Otherwise, \tcode{\placeholder{ref-view}\{E\}} if that
expression is well-formed, where \tcode{\placeholder{ref-view}}
is the exposition-only \libconcept{View} specified below.
\item Otherwise, \tcode{ref_view\{E\}} if that expression is well-formed.

\item Otherwise, \tcode{subrange\{E\}}.
\end{itemize}

\rSec3[range.view.ref]{\placeholder{ref-view}}
\rSec3[range.ref.view]{Class template \tcode{ref_view}}

\pnum
\tcode{ref_view} is a \tcode{View} of the elements of some other \tcode{Range}.
\begin{codeblock}
namespace std::ranges {
template<Range R>
requires is_object_v<R>
class @\placeholder{ref-view}@ : public view_interface<@\placeholder{ref-view}@<R>> {
class ref_view : public view_interface<ref_view<R>> {
private:
R* r_ = nullptr; // \expos
public:
constexpr @\placeholdernc{ref-view}@() noexcept = default;
constexpr ref_view() noexcept = default;

template<@\placeholder{not-same-as}@<@\placeholder{ref-view}@> T>
template<@\placeholder{not-same-as}@<ref_view> T>
requires @\seebelow@
constexpr @\placeholder{ref-view}@(T&& t);
constexpr ref_view(T&& t);

constexpr R& base() const { return *r_; }

Expand All @@ -2385,22 +2435,22 @@
constexpr auto data() const requires ContiguousRange<R>
{ return ranges::data(*r_); }

friend constexpr iterator_t<R> begin(@\placeholder{ref-view}@ r)
friend constexpr iterator_t<R> begin(ref_view r)
{ return r.begin(); }

friend constexpr sentinel_t<R> end(@\placeholder{ref-view}@ r)
friend constexpr sentinel_t<R> end(ref_view r)
{ return r.end(); }
};
template<class R>
@\placeholder{ref-view}@(R&) -> @\placeholder{ref-view}@<R>;
ref_view(R&) -> ref_view<R>;
}
\end{codeblock}

\indexlibrary{\idxcode{\placeholder{ref-view}}!\idxcode{\placeholder{ref-view}}}%
\indexlibrary{\idxcode{ref_view}}%
\begin{itemdecl}
template<@\placeholder{not-same-as}@<@\placeholder{ref-view}@> T>
template<@\placeholder{not-same-as}@<ref_view> T>
requires @\seebelow@
constexpr @\placeholder{ref-view}@(T&& t);
constexpr ref_view(T&& t);
\end{itemdecl}

\begin{itemdescr}
Expand Down Expand Up @@ -4308,7 +4358,7 @@

template<InputRange V, ForwardRange Pattern>
requires View<V> && View<Pattern> &&
IndirectlyComparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to<>> &&
IndirectlyComparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
(ForwardRange<V> || @\placeholdernc{tiny-range}@<Pattern>)
class split_view : public view_interface<split_view<V, Pattern>> {
private:
Expand Down Expand Up @@ -5113,5 +5163,32 @@
The name \tcode{view::reverse} denotes a
range adaptor object\iref{range.adaptor.object}.
For some subexpression \tcode{E}, the expression
\tcode{view::reverse(E)} is expression-equivalent to
\tcode{reverse_view\{E\}}.
\tcode{view::reverse(E)} is expression-equivalent to:
\begin{itemize}
\item
If the type of \tcode{E} is
a (possibly cv-qualified) specialization of \tcode{reverse_view},
equivalent to \tcode{E.base()}.
\item
Otherwise, if the type of \tcode{E} is cv-qualified
\begin{codeblock}
subrange<reverse_iterator<I>, reverse_iterator<I>, K>
\end{codeblock}
for some iterator type \tcode{I} and
value \tcode{K} of type \tcode{subrange_kind},
\begin{itemize}
\item
if \tcode{K} is \tcode{subrange_kind::sized}, equivalent to:
\begin{codeblock}
subrange<I, I, K>(E.end().base(), E.begin().base(), E.size())
\end{codeblock}
\item
otherwise, equivalent to:
\begin{codeblock}
subrange<I, I, K>(E.end().base(), E.begin().base())
\end{codeblock}
\end{itemize}
However, in either case \tcode{E} is evaluated only once.
\item
Otherwise, equivalent to \tcode{reverse_view\{E\}}.
\end{itemize}
Loading