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.129.124]) by sourceware.org (Postfix) with ESMTPS id 3AE84388460E for ; Mon, 14 Nov 2022 15:13:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3AE84388460E 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=1668438786; 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=Pq+czATmKna/O7/HAg/TNFe3PRKhJEme/26GE2vK9foCzRMTa2ahBKZr9NzhB7M5s+CqBU mTzXZki8MjmW9PvokMwKiah2Tcs9y1SoKAXiqnh+Bn1JAcYuJmsaxuAWYNFWhOzUDwpsGU xqkVQ2U0hbV6Dj4uBvdAlFple10X5Qk= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-193-lE_YjbRAPD-O9Ler1E_yuQ-1; Mon, 14 Nov 2022 10:13:05 -0500 X-MC-Unique: lE_YjbRAPD-O9Ler1E_yuQ-1 Received: by mail-ej1-f71.google.com with SMTP id xc12-20020a170907074c00b007416699ea14so5576228ejb.19 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=c7OBLf5VnqsPcvy6VsFyky0BDjqqi6ZyC2VaXYEs1VM7iOYlw39C6LQY/zG/6dTH/o w6g8Xu23GhlCrDPmjD9G1jAM37HTrntX3e89TUdgbYeshcy3pzzyrASiKwS2+H3Mzxy1 Dd1LU7D5MNZqpACyp/cGBVtIesUJjBY8drriHTm1VgFXJL8UMk+fehZD32Q8FPj9h2l9 mCSFT2zaLgaJCKl/PbbfKg/x7DLG1pG6WhRiLsPkMoDbxxxo6ZQ5EaPZtpAx8+q3+Nqj nxjIOQHvAereYZNtXt4A4QdawTW5bL7XRJ66ICnl2UcuDewQlCJlh8svVEBxwvI0WONe UTvQ== X-Gm-Message-State: ANoB5pkTDYhJCo1MnBFymrqFzCEe+II/X63rp02LpuJ8kfE2UX6OopCC azARNrsBas0tcO/yK+dF+SYxxramZqDKLPyem+xwrcW0BWx3pwAGEvWuwEYSOt3ZyK/FV84Bv4w xIkLQcf6nZJt8mGdkwEqKepTwvyE0aWH8bw== X-Received: by 2002:a05:6402:5511:b0:468:dc9:ec08 with SMTP id fi17-20020a056402551100b004680dc9ec08mr1802628edb.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=-11.9 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=ham 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