On Mon, 14 Nov 2022 at 15:11, Patrick Palka <[email protected]> wrote:
>
> On Mon, 14 Nov 2022, Jonathan Wakely wrote:
>
> > On Mon, 14 Nov 2022 at 10:17, Daniel Krügler <[email protected]>
> > wrote:
> > >
> > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via
> > > Libstdc++ <[email protected]>:
> > > >
> > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++
> > > > <[email protected]> wrote:
> > > > >
> > > > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> > > > >
> > > > > libstdc++-v3/ChangeLog:
> > > > >
> > > > > * include/bits/ranges_algo.h (out_value_result): Define.
> > > > > (iota_result): Define.
> > > > > (__iota_fn, iota): Define.
> > > > > * testsuite/25_algorithms/iota/1.cc: New test.
> > > > > ---
> > > > > libstdc++-v3/include/bits/ranges_algo.h | 48
> > > > > +++++++++++++++++++
> > > > > .../testsuite/25_algorithms/iota/1.cc | 29 +++++++++++
> > > > > 2 files changed, 77 insertions(+)
> > > > > create mode 100644 libstdc++-v3/testsuite/25_algorithms/iota/1.cc
> > > > >
> > > > > diff --git a/libstdc++-v3/include/bits/ranges_algo.h
> > > > > b/libstdc++-v3/include/bits/ranges_algo.h
> > > > > index da0ca981dc3..f003117c569 100644
> > > > > --- a/libstdc++-v3/include/bits/ranges_algo.h
> > > > > +++ b/libstdc++-v3/include/bits/ranges_algo.h
> > > > > @@ -3517,6 +3517,54 @@ namespace ranges
> > > > > };
> > > > >
> > > > > inline constexpr __contains_subrange_fn contains_subrange{};
> > > > > +
> > > > > + template<typename _Out, typename _Tp>
> > > > > + struct out_value_result
> > > > > + {
> > > > > + [[no_unique_address]] _Out out;
> > > > > + [[no_unique_address]] _Tp value;
> > > > > +
> > > > > + template<typename _Out2, typename _Tp2>
> > > > > + requires convertible_to<const _Out&, _Out2>
> > > > > + && convertible_to<const _Tp&, _Tp2>
> > > > > + constexpr
> > > > > + operator out_value_result<_Out2, _Tp2>() const &
> > > > > + { return {out, value}; }
> > > > > +
> > > > > + template<typename _Out2, typename _Tp2>
> > > > > + requires convertible_to<_Out, _Out2>
> > > > > + && convertible_to<_Tp, _Tp2>
> > > > > + constexpr
> > > > > + operator out_value_result<_Out2, _Tp2>() &&
> > > > > + { return {std::move(out), std::move(value)}; }
> > > > > + };
> > > > > +
> > > > > + template<typename _Out, typename _Tp>
> > > > > + using iota_result = out_value_result<_Out, _Tp>;
> > > > > +
> > > > > + struct __iota_fn
> > > > > + {
> > > > > + template<input_or_output_iterator _Out, sentinel_for<_Out>
> > > > > _Sent, weakly_incrementable _Tp>
> > > > > + requires indirectly_writable<_Out, const _Tp&>
> > > > > + constexpr iota_result<_Out, _Tp>
> > > > > + operator()(_Out __first, _Sent __last, _Tp __value) const
> > > > > + {
> > > > > + while (__first != __last)
> > > > > + {
> > > > > + *__first = static_cast<add_const_t<_Tp>&>(__value);
> > > >
> > > > Is this any different to const_cast<const _Tp&>(__value) ?
> > >
> > > I think it is. const_cast<const _Tp&> can potentially mean the removal
> > > of volatile,
> >
> > True.
> >
> > > so I would always look with suspicion on const_cast<const
> > > _Tp&>, while static_cast is clearer. Alternatively, as_const could be
> > > used, which does add_const_t.
> >
> > Which means evaluating the add_const trait *and* overload resolution
> > for as_const* *and* a runtime function call.
> >
> > Let's go with static_cast<const _Tp&>.
>
> Sounds good, like so?
OK for trunk, thanks.
>
> -- >8 --
>
>
> Subject: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1
>
> libstdc++-v3/ChangeLog:
>
> * include/bits/ranges_algo.h (out_value_result): Define.
> (iota_result): Define.
> (__iota_fn, iota): Define.
> * testsuite/25_algorithms/iota/1.cc: New test.
> ---
> libstdc++-v3/include/bits/ranges_algo.h | 48 +++++++++++++++++++
> .../testsuite/25_algorithms/iota/1.cc | 29 +++++++++++
> 2 files changed, 77 insertions(+)
> create mode 100644 libstdc++-v3/testsuite/25_algorithms/iota/1.cc
>
> diff --git a/libstdc++-v3/include/bits/ranges_algo.h
> b/libstdc++-v3/include/bits/ranges_algo.h
> index 11206bdbcaa..f75735f02cb 100644
> --- a/libstdc++-v3/include/bits/ranges_algo.h
> +++ b/libstdc++-v3/include/bits/ranges_algo.h
> @@ -3517,6 +3517,54 @@ namespace ranges
> };
>
> inline constexpr __contains_subrange_fn contains_subrange{};
> +
> + template<typename _Out, typename _Tp>
> + struct out_value_result
> + {
> + [[no_unique_address]] _Out out;
> + [[no_unique_address]] _Tp value;
> +
> + template<typename _Out2, typename _Tp2>
> + requires convertible_to<const _Out&, _Out2>
> + && convertible_to<const _Tp&, _Tp2>
> + constexpr
> + operator out_value_result<_Out2, _Tp2>() const &
> + { return {out, value}; }
> +
> + template<typename _Out2, typename _Tp2>
> + requires convertible_to<_Out, _Out2>
> + && convertible_to<_Tp, _Tp2>
> + constexpr
> + operator out_value_result<_Out2, _Tp2>() &&
> + { return {std::move(out), std::move(value)}; }
> + };
> +
> + template<typename _Out, typename _Tp>
> + using iota_result = out_value_result<_Out, _Tp>;
> +
> + struct __iota_fn
> + {
> + template<input_or_output_iterator _Out, sentinel_for<_Out> _Sent,
> weakly_incrementable _Tp>
> + requires indirectly_writable<_Out, const _Tp&>
> + constexpr iota_result<_Out, _Tp>
> + operator()(_Out __first, _Sent __last, _Tp __value) const
> + {
> + while (__first != __last)
> + {
> + *__first = static_cast<const _Tp&>(__value);
> + ++__first;
> + ++__value;
> + }
> + return {std::move(__first), std::move(__value)};
> + }
> +
> + template<weakly_incrementable _Tp, output_range<const _Tp&> _Range>
> + constexpr iota_result<borrowed_iterator_t<_Range>, _Tp>
> + operator()(_Range&& __r, _Tp __value) const
> + { return (*this)(ranges::begin(__r), ranges::end(__r),
> std::move(__value)); }
> + };
> +
> + inline constexpr __iota_fn iota{};
> #endif // C++23
> } // namespace ranges
>
> diff --git a/libstdc++-v3/testsuite/25_algorithms/iota/1.cc
> b/libstdc++-v3/testsuite/25_algorithms/iota/1.cc
> new file mode 100644
> index 00000000000..ad2bf08adf5
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/25_algorithms/iota/1.cc
> @@ -0,0 +1,29 @@
> +// { dg-options "-std=gnu++23" }
> +// { dg-do run { target c++23 } }
> +
> +#include <algorithm>
> +#include <testsuite_hooks.h>
> +#include <testsuite_iterators.h>
> +
> +namespace ranges = std::ranges;
> +
> +void
> +test01()
> +{
> + int x[3] = {};
> + __gnu_test::test_output_range<int> rx(x);
> + auto r0 = ranges::iota(rx, 0);
> + VERIFY( r0.out.ptr == x+3 );
> + VERIFY( r0.value == 3 );
> + VERIFY( ranges::equal(x, (int[]){0,1,2}) );
> + auto r1 = ranges::iota(x, x+2, 5);
> + VERIFY( r1.out == x+2 );
> + VERIFY( r1.value == 7 );
> + VERIFY( ranges::equal(x, (int[]){5,6,2}) );
> +}
> +
> +int
> +main()
> +{
> + test01();
> +}
> --
> 2.38.1.420.g319605f8f0