From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 2EE5D385802C; Tue, 11 Jan 2022 13:23:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2EE5D385802C MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-6438] libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: accccf7dee8e040f25421f4c5f41894c8ea974c1 X-Git-Newrev: 11d3e8f4364b05e2746689c6d0f573594f6ddb74 Message-Id: <20220111132358.2EE5D385802C@sourceware.org> Date: Tue, 11 Jan 2022 13:23:58 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Jan 2022 13:23:58 -0000 https://gcc.gnu.org/g:11d3e8f4364b05e2746689c6d0f573594f6ddb74 commit r12-6438-g11d3e8f4364b05e2746689c6d0f573594f6ddb74 Author: Jonathan Wakely Date: Mon Jan 10 16:51:49 2022 +0000 libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574) 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. Diff: --- libstdc++-v3/include/bits/stl_iterator.h | 34 ++++++++++++---------- .../testsuite/24_iterators/common_iterator/1.cc | 9 +++++- .../testsuite/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(); }