From patchwork Tue Jan 11 13:25:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1578437 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=bked0osi; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JYBKb1wGCz9s0r for ; Wed, 12 Jan 2022 00:26:10 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F3C8F38A9415 for ; Tue, 11 Jan 2022 13:26:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F3C8F38A9415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641907568; bh=bjR3Wc4j9TH3I88dhuQ3DeAz5/otdfjOBAitLdKlWzE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=bked0osibyo5Z+8v4ICA0JSLFh976iMYL+SULwW+BlOF057r+JWEn+FtXeBWbnaDx 6Rjomp3LSMYN1WIY5CIg88lg4WRhk+2UnldTfHAnT+6LgY8VY0H6zto01JqNYf1JRG 5aNidleJ+vb7hchewzzyjShBO0k0Fb3VOY89Y4RQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 79F413858D39 for ; Tue, 11 Jan 2022 13:25:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 79F413858D39 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-284-6l41z64SOL2uWVume7-lKg-1; Tue, 11 Jan 2022 08:25:41 -0500 X-MC-Unique: 6l41z64SOL2uWVume7-lKg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0AAA81926DA2; Tue, 11 Jan 2022 13:25:41 +0000 (UTC) Received: from localhost (unknown [10.33.36.252]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A6D47EBCA; Tue, 11 Jan 2022 13:25:40 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574) Date: Tue, 11 Jan 2022 13:25:39 +0000 Message-Id: <20220111132539.2559644-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-linux. Pushed to trunk. This library issue was approved in the October 2021 plenary. libstdc++-v3/ChangeLog: * include/bits/stl_iterator.h (common_iterator): Add constexpr to all member functions (LWG 3574). * testsuite/24_iterators/common_iterator/1.cc: Evaluate some tests as constant expressions. * testsuite/24_iterators/common_iterator/2.cc: Likewise. --- libstdc++-v3/include/bits/stl_iterator.h | 34 ++++++++++--------- .../24_iterators/common_iterator/1.cc | 9 ++++- .../24_iterators/common_iterator/2.cc | 14 ++++---- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index cfeb7981ed8..24a71ea55af 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -77,11 +77,12 @@ # define __cpp_lib_array_constexpr 201803L #endif -#if __cplusplus > 201703L +#if __cplusplus >= 202002L # include # include # include # include +# include #endif namespace std _GLIBCXX_VISIBILITY(default) @@ -1938,7 +1939,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } - common_iterator& + constexpr common_iterator& operator=(const common_iterator& __x) noexcept(is_nothrow_copy_assignable_v<_It> && is_nothrow_copy_assignable_v<_Sent> @@ -1953,7 +1954,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION && convertible_to && assignable_from<_It&, const _It2&> && assignable_from<_Sent&, const _Sent2&> - common_iterator& + constexpr common_iterator& operator=(const common_iterator<_It2, _Sent2>& __x) noexcept(is_nothrow_constructible_v<_It, const _It2&> && is_nothrow_constructible_v<_Sent, const _Sent2&> @@ -1973,7 +1974,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_index = -1; [[fallthrough]]; case 0b1001: - ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent); + std::construct_at(std::__addressof(_M_sent), _Sent(__x._M_sent)); _M_index = 1; break; case 0b0100: @@ -1981,7 +1982,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_index = -1; [[fallthrough]]; case 0b1000: - ::new((void*)std::__addressof(_M_it)) _It(__x._M_it); + std::construct_at(std::__addressof(_M_it), _It(__x._M_it)); _M_index = 0; break; default: @@ -1991,6 +1992,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + constexpr ~common_iterator() { switch (_M_index) @@ -2005,7 +2007,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] - decltype(auto) + constexpr decltype(auto) operator*() { __glibcxx_assert(_M_index == 0); @@ -2013,7 +2015,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] - decltype(auto) + constexpr decltype(auto) operator*() const requires __detail::__dereferenceable { __glibcxx_assert(_M_index == 0); @@ -2021,7 +2023,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] - decltype(auto) + constexpr decltype(auto) operator->() const requires __detail::__common_iter_has_arrow<_It> { __glibcxx_assert(_M_index == 0); @@ -2036,7 +2038,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __arrow_proxy{*_M_it}; } - common_iterator& + constexpr common_iterator& operator++() { __glibcxx_assert(_M_index == 0); @@ -2044,7 +2046,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } - decltype(auto) + constexpr decltype(auto) operator++(int) { __glibcxx_assert(_M_index == 0); @@ -2066,7 +2068,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Sent2> requires sentinel_for<_Sent, _It2> - friend bool + friend constexpr bool operator== [[nodiscard]] (const common_iterator& __x, const common_iterator<_It2, _Sent2>& __y) { @@ -2088,7 +2090,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Sent2> requires sentinel_for<_Sent, _It2> && equality_comparable_with<_It, _It2> - friend bool + friend constexpr bool operator== [[nodiscard]] (const common_iterator& __x, const common_iterator<_It2, _Sent2>& __y) { @@ -2111,7 +2113,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _It2, sized_sentinel_for<_It> _Sent2> requires sized_sentinel_for<_Sent, _It2> - friend iter_difference_t<_It2> + friend constexpr iter_difference_t<_It2> operator- [[nodiscard]] (const common_iterator& __x, const common_iterator<_It2, _Sent2>& __y) { @@ -2133,7 +2135,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } [[nodiscard]] - friend iter_rvalue_reference_t<_It> + friend constexpr iter_rvalue_reference_t<_It> iter_move(const common_iterator& __i) noexcept(noexcept(ranges::iter_move(std::declval()))) requires input_iterator<_It> @@ -2143,7 +2145,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template _It2, typename _Sent2> - friend void + friend constexpr void iter_swap(const common_iterator& __x, const common_iterator<_It2, _Sent2>& __y) noexcept(noexcept(ranges::iter_swap(std::declval(), @@ -2158,7 +2160,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Sent2> friend class common_iterator; - bool _M_has_value() const noexcept { return _M_index < 2; } + constexpr bool _M_has_value() const noexcept { return _M_index < 2; } union { diff --git a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc index 2a9828031b4..484d0cc2301 100644 --- a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc +++ b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc @@ -88,7 +88,7 @@ test02() VERIFY( i == (&i - out) ); } -void +constexpr bool test03() { int arr[2] = { 1, 2 }; @@ -117,6 +117,9 @@ test03() VERIFY( (j - end) == -2 ); VERIFY( (j - i) == 0 ); + if (std::is_constant_evaluated()) + return true; + try { struct S { operator const int*() const { throw 1; } }; @@ -126,8 +129,12 @@ test03() catch (int) { } + + return true; } +static_assert( test03() ); + void test04() { diff --git a/libstdc++-v3/testsuite/24_iterators/common_iterator/2.cc b/libstdc++-v3/testsuite/24_iterators/common_iterator/2.cc index 012546a4645..9ec303e526d 100644 --- a/libstdc++-v3/testsuite/24_iterators/common_iterator/2.cc +++ b/libstdc++-v3/testsuite/24_iterators/common_iterator/2.cc @@ -32,18 +32,18 @@ struct iterator using difference_type = std::ptrdiff_t; using reference = value; - value operator*() const { return value{counter}; } + constexpr value operator*() const { return value{counter}; } - iterator& operator++() { ++counter; return *this; } + constexpr iterator& operator++() { ++counter; return *this; } - iterator operator++(int) { auto i = *this; ++counter; return i; } + constexpr iterator operator++(int) { auto i = *this; ++counter; return i; } - bool operator==(sentinel s) const { return counter == s.limit; } + constexpr bool operator==(sentinel s) const { return counter == s.limit; } int counter = 0; }; -void +constexpr bool test01() { iterator i; @@ -54,10 +54,12 @@ test01() VERIFY( begin->n == 1 ); ++begin; VERIFY( begin == end ); + + return true; } int main() { - test01(); + constexpr bool b1 = test01(); }