From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id EB7753834E41; Fri, 22 Jul 2022 07:07:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EB7753834E41 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 r11-10163] libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 0bc62cb594d6459005e439fcf7769ecaa48f59b2 X-Git-Newrev: a4e648fab46e83bde7008f2633e64fae39561c61 Message-Id: <20220722070745.EB7753834E41@sourceware.org> Date: Fri, 22 Jul 2022 07:07:45 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 Jul 2022 07:07:46 -0000 https://gcc.gnu.org/g:a4e648fab46e83bde7008f2633e64fae39561c61 commit r11-10163-ga4e648fab46e83bde7008f2633e64fae39561c61 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. This backport for gcc-11 also includes some [[nodiscard]] attributes from earlier commits on trunk. 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. (cherry picked from commit 11d3e8f4364b05e2746689c6d0f573594f6ddb74) Diff: --- libstdc++-v3/include/bits/stl_iterator.h | 50 ++++++++++++---------- .../testsuite/24_iterators/common_iterator/1.cc | 9 +++- .../testsuite/24_iterators/common_iterator/2.cc | 14 +++--- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 38a828e633e..96ca48e8130 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) @@ -1839,7 +1840,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> @@ -1854,7 +1855,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&> @@ -1874,7 +1875,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: @@ -1882,7 +1883,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: @@ -1892,6 +1893,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } + constexpr ~common_iterator() { switch (_M_index) @@ -1905,21 +1907,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } - decltype(auto) + [[nodiscard]] + constexpr decltype(auto) operator*() { __glibcxx_assert(_M_index == 0); return *_M_it; } - decltype(auto) + [[nodiscard]] + constexpr decltype(auto) operator*() const requires __detail::__dereferenceable { __glibcxx_assert(_M_index == 0); return *_M_it; } - decltype(auto) + [[nodiscard]] + constexpr decltype(auto) operator->() const requires __detail::__common_iter_has_arrow<_It> { __glibcxx_assert(_M_index == 0); @@ -1934,7 +1939,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __arrow_proxy{*_M_it}; } - common_iterator& + constexpr common_iterator& operator++() { __glibcxx_assert(_M_index == 0); @@ -1942,7 +1947,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return *this; } - decltype(auto) + constexpr decltype(auto) operator++(int) { __glibcxx_assert(_M_index == 0); @@ -1964,9 +1969,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Sent2> requires sentinel_for<_Sent, _It2> - friend bool - operator==(const common_iterator& __x, - const common_iterator<_It2, _Sent2>& __y) + friend constexpr bool + operator== [[nodiscard]] (const common_iterator& __x, + const common_iterator<_It2, _Sent2>& __y) { switch(__x._M_index << 2 | __y._M_index) { @@ -1986,9 +1991,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _Sent2> requires sentinel_for<_Sent, _It2> && equality_comparable_with<_It, _It2> - friend bool - operator==(const common_iterator& __x, - const common_iterator<_It2, _Sent2>& __y) + friend constexpr bool + operator== [[nodiscard]] (const common_iterator& __x, + const common_iterator<_It2, _Sent2>& __y) { switch(__x._M_index << 2 | __y._M_index) { @@ -2009,9 +2014,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template _It2, sized_sentinel_for<_It> _Sent2> requires sized_sentinel_for<_Sent, _It2> - friend iter_difference_t<_It2> - operator-(const common_iterator& __x, - const common_iterator<_It2, _Sent2>& __y) + friend constexpr iter_difference_t<_It2> + operator- [[nodiscard]] (const common_iterator& __x, + const common_iterator<_It2, _Sent2>& __y) { switch(__x._M_index << 2 | __y._M_index) { @@ -2030,7 +2035,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } - friend iter_rvalue_reference_t<_It> + [[nodiscard]] + friend constexpr iter_rvalue_reference_t<_It> iter_move(const common_iterator& __i) noexcept(noexcept(ranges::iter_move(std::declval()))) requires input_iterator<_It> @@ -2040,7 +2046,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(), @@ -2055,7 +2061,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 fb8c7adab27..db38230f323 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 80f4f2b0151..f8580b57890 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(); }