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 AE9013858D1E for ; Mon, 14 Nov 2022 10:08:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AE9013858D1E 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=1668420509; 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: in-reply-to:in-reply-to:references:references; bh=4A1PsCCdd4Y576m9Lv+EGvbU9y82cGNZbanJEh59qkA=; b=VMDPyNBagr2ls8oWQqr8DIkmvUXq0rzWmo1iN1cdYe18pqRuduxUs0JMrF9l1/f8+BcxEd gp7OYQZ3hk5JYTOvBT83AbfLq0vK6+xM/QVUSAogo+MRFxrKTdEXO0l7cmXq7f2c3fyAT/ Pm+uZFbxa4P8zCYlCC4TFJr6voS5UNI= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-487-aaipUisFNciEAOTjaixBxw-1; Mon, 14 Nov 2022 05:08:26 -0500 X-MC-Unique: aaipUisFNciEAOTjaixBxw-1 Received: by mail-ed1-f71.google.com with SMTP id dz9-20020a0564021d4900b0045d9a3aded4so8066084edb.22 for ; Mon, 14 Nov 2022 02:08:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=4A1PsCCdd4Y576m9Lv+EGvbU9y82cGNZbanJEh59qkA=; b=det+8bSD57609nntx4Maw1hr79TPAVPh/1TgNH8cC4CAr1uq6V+/HY9ygIDxO4Hv2/ OYlJ36jNOVexVj/IXqLrxNt1wmkAuHbQbVVewRZBK2c37eLC4uAoa3v9VnrOwya8JrZQ DBv4UxzKkLZHTRfBwQ4d/glknMQK5yRg96ZNcl3mgNrdM151cFfU6HZKvXggI3HP3N0n Ol/K+JIIx8WO7qfMrG/TDBKVBz2JL4l2iL+cso0vzMTCzmkEtXIL6tejO6Vnko6x+T5S hkvsZrGKN0GcysfY9JmGzmXb1iVDvkcwmZAk3JlUGtuUKSsWdbTbh245ienTXma2C5uY fT0A== X-Gm-Message-State: ANoB5pmP8ezVxVRgHO611o4eBrWPe5fUkH896Q4wL/qhB6qk5/8aWOpJ wF9B3JeOiGeSmkTpO4/cwzj+QyfgsqcczDDTb4v/1JMPgJBUDyZPs/MuNZ6WVfm4U/XHjY6qu2u Ym8w8AD9DbGIBybVS2IEmnj40s42RnLQ= X-Received: by 2002:a17:907:9258:b0:78d:8dfb:10ad with SMTP id kb24-20020a170907925800b0078d8dfb10admr10189183ejb.289.1668420505782; Mon, 14 Nov 2022 02:08:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf6/LzECo7ONW903FiHow3tpvCAooOVdNxBR+1QOyhT/J8efkpSG7pydkUvUFX2VVL2mzjZmBKoZmJlf8zyWDeQ= X-Received: by 2002:a17:907:9258:b0:78d:8dfb:10ad with SMTP id kb24-20020a170907925800b0078d8dfb10admr10189174ejb.289.1668420505587; Mon, 14 Nov 2022 02:08:25 -0800 (PST) MIME-Version: 1.0 References: <20221114045047.362745-1-ppalka@redhat.com> <20221114045047.362745-2-ppalka@redhat.com> In-Reply-To: <20221114045047.362745-2-ppalka@redhat.com> From: Jonathan Wakely Date: Mon, 14 Nov 2022 10:08:14 +0000 Message-ID: Subject: Re: [PATCH 2/3] libstdc++: Implement ranges::iota from P2440R1 To: Patrick Palka Cc: 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" X-Spam-Status: No, score=-12.1 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 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.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 > + 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 = 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 != __last) > + { > + *__first = static_cast&>(__value); Is this any different to const_cast(__value) ? We know _Tp must be the same as decay_t<_Tp> and non-void, because it's passed by value, and therefore I think is_same_v> is always true, isn't it? We don't need to care about people saying ranges::iota.operator()(o,s,t), those people are animals. We would just change the function parameter to const _Tp which would mean that *_first = __value; always uses a const lvalue, but maybe that's a bit too subtle. The cast makes it more explicit what's happening, especially the const_cast version. > + ++__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++-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 > +#include > +#include > + > +namespace ranges = std::ranges; > + > +void > +test01() > +{ > + int x[3] = {}; > + __gnu_test::test_output_range 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 >