From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8BDEC385558F for ; Mon, 14 Nov 2022 15:13:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8BDEC385558F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668438788; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gPbULar7krx4IMSFvETs3U9RwdPrWkyEdMnQQf8AQ18=; b=fqATcVMVCDrXd+P31YsmTDtrgOq03RtV9HE9/iUtRQSrHQaO8xLHFeVqqf8CNjqzOh3kO5 it6GG/WuxCf9cwBv6KgBQiPxo6TdyF7ay0O+HvFdSmlocZBzXX7tfnu1x7JP4fz3qWqRmQ A0jSL0TM3a0ZoQmNJ3MGMw+60zuHElU= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-416-qBmsqPOmP6qiTzC6bprErA-1; Mon, 14 Nov 2022 10:13:05 -0500 X-MC-Unique: qBmsqPOmP6qiTzC6bprErA-1 Received: by mail-ed1-f69.google.com with SMTP id m7-20020a056402430700b0045daff6ee5dso8458809edc.10 for ; Mon, 14 Nov 2022 07:13:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gPbULar7krx4IMSFvETs3U9RwdPrWkyEdMnQQf8AQ18=; b=oXdi75ExnaLWaCG/j2BMXfIskhdpKKKQo8P6M56LMwkF572Vk77W2fLtL+xE0Kfkvz +sE9Pj9Ihzv6I/Vd2r0M0j426cRHHklb3EgSbrffbM+tmRpcXwo6NJUwkiwUd4TuDvnj lBUfkARAj2QChkjV/QV2LmHqRkDFnY/hvsmMwQkt3rpDZCEyBAYaqXOXBn6AVX5k1YLj NcHZYhg5V2/ZmAKbV3XqFD+Qgv2oR0sQB+eIgUk/vNW7spvb5HyuwIVnYgMywhy0hXWk dKUrCQX2HvY8Hoc9snMJtWxq+7R+BgFU61qwvNIteGaNOnZiLpyLx61ct9msCjEHRo5b QIQw== X-Gm-Message-State: ANoB5pnh7IuivsiN3yu6bcPn2kdwnB6PWvWr7g8bHgoT1fZV4/tnJYHK IxKaA7NKy3FgsURhWCbmVnMpRYg3yhP8M75JlIjlR7RfK8uMzpsGJZ6va5geMbZgMrv2uUPqSyS OrAJQYjineJ6t6bEaUBSwG/2pq8NEocc= X-Received: by 2002:a05:6402:5511:b0:468:dc9:ec08 with SMTP id fi17-20020a056402551100b004680dc9ec08mr1802627edb.17.1668438784456; Mon, 14 Nov 2022 07:13:04 -0800 (PST) X-Google-Smtp-Source: AA0mqf4PeQyKbh3f3DKD49iQ/U4friQrYyMMOCPpN9I+hiGLfHbf9gPe8n/MaX10G7isqGMHTMelIJH00rXz5EPWfkA= X-Received: by 2002:a05:6402:5511:b0:468:dc9:ec08 with SMTP id fi17-20020a056402551100b004680dc9ec08mr1802606edb.17.1668438784264; Mon, 14 Nov 2022 07:13:04 -0800 (PST) MIME-Version: 1.0 References: <20221114045047.362745-1-ppalka@redhat.com> <20221114045047.362745-2-ppalka@redhat.com> <0695bdea-897c-7c35-261b-5c426b594144@idea> In-Reply-To: <0695bdea-897c-7c35-261b-5c426b594144@idea> From: Jonathan Wakely Date: Mon, 14 Nov 2022 15:12:53 +0000 Message-ID: Subject: Re: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1 To: Patrick Palka Cc: =?UTF-8?Q?Daniel_Kr=C3=BCgler?= , gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 14 Nov 2022 at 15:11, Patrick Palka wrote: > > On Mon, 14 Nov 2022, Jonathan Wakely wrote: > > > On Mon, 14 Nov 2022 at 10:17, Daniel Kr=C3=BCgler wrote: > > > > > > Am Mo., 14. Nov. 2022 um 11:09 Uhr schrieb Jonathan Wakely via > > > Libstdc++ : > > > > > > > > On Mon, 14 Nov 2022 at 04:52, Patrick Palka via Libstdc++ > > > > 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.c= c > > > > > > > > > > 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 > > > > > + struct out_value_result > > > > > + { > > > > > + [[no_unique_address]] _Out out; > > > > > + [[no_unique_address]] _Tp value; > > > > > + > > > > > + template > > > > > + requires convertible_to > > > > > + && convertible_to > > > > > + constexpr > > > > > + operator out_value_result<_Out2, _Tp2>() const & > > > > > + { return {out, value}; } > > > > > + > > > > > + template > > > > > + requires convertible_to<_Out, _Out2> > > > > > + && convertible_to<_Tp, _Tp2> > > > > > + constexpr > > > > > + operator out_value_result<_Out2, _Tp2>() && > > > > > + { return {std::move(out), std::move(value)}; } > > > > > + }; > > > > > + > > > > > + template > > > > > + using iota_result =3D out_value_result<_Out, _Tp>; > > > > > + > > > > > + struct __iota_fn > > > > > + { > > > > > + template _= 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 !=3D __last) > > > > > + { > > > > > + *__first =3D static_cast&>(__value); > > > > > > > > Is this any different to const_cast(__value) ? > > > > > > I think it is. const_cast can potentially mean the remova= l > > > of volatile, > > > > True. > > > > > so I would always look with suspicion on const_cast > > _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. > > 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/inclu= de/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 > + struct out_value_result > + { > + [[no_unique_address]] _Out out; > + [[no_unique_address]] _Tp value; > + > + template > + requires convertible_to > + && convertible_to > + constexpr > + operator out_value_result<_Out2, _Tp2>() const & > + { return {out, value}; } > + > + template > + requires convertible_to<_Out, _Out2> > + && convertible_to<_Tp, _Tp2> > + constexpr > + operator out_value_result<_Out2, _Tp2>() && > + { return {std::move(out), std::move(value)}; } > + }; > + > + template > + using iota_result =3D out_value_result<_Out, _Tp>; > + > + struct __iota_fn > + { > + template _Sent, we= akly_incrementable _Tp> > + requires indirectly_writable<_Out, const _Tp&> > + constexpr iota_result<_Out, _Tp> > + operator()(_Out __first, _Sent __last, _Tp __value) const > + { > + while (__first !=3D __last) > + { > + *__first =3D static_cast(__value); > + ++__first; > + ++__value; > + } > + return {std::move(__first), std::move(__value)}; > + } > + > + template _Range> > + constexpr iota_result, _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++-v= 3/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=3Dgnu++23" } > +// { dg-do run { target c++23 } } > + > +#include > +#include > +#include > + > +namespace ranges =3D std::ranges; > + > +void > +test01() > +{ > + int x[3] =3D {}; > + __gnu_test::test_output_range rx(x); > + auto r0 =3D ranges::iota(rx, 0); > + VERIFY( r0.out.ptr =3D=3D x+3 ); > + VERIFY( r0.value =3D=3D 3 ); > + VERIFY( ranges::equal(x, (int[]){0,1,2}) ); > + auto r1 =3D ranges::iota(x, x+2, 5); > + VERIFY( r1.out =3D=3D x+2 ); > + VERIFY( r1.value =3D=3D 7 ); > + VERIFY( ranges::equal(x, (int[]){5,6,2}) ); > +} > + > +int > +main() > +{ > + test01(); > +} > -- > 2.38.1.420.g319605f8f0