public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-10163] libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574)
@ 2022-07-22 7:07 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2022-07-22 7:07 UTC (permalink / raw)
To: gcc-cvs, libstdc++-cvs
https://gcc.gnu.org/g:a4e648fab46e83bde7008f2633e64fae39561c61
commit r11-10163-ga4e648fab46e83bde7008f2633e64fae39561c61
Author: Jonathan Wakely <jwakely@redhat.com>
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 <compare>
# include <new>
# include <bits/exception_defines.h>
# include <bits/iterator_concepts.h>
+# include <bits/stl_construct.h>
#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<const _Sent2&, _Sent>
&& 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<const _It>
{
__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<typename _It2, sentinel_for<_It> _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<typename _It2, sentinel_for<_It> _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<sized_sentinel_for<_It> _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<const _It&>())))
requires input_iterator<_It>
@@ -2040,7 +2046,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<indirectly_swappable<_It> _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<const _It&>(),
@@ -2055,7 +2061,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<input_or_output_iterator _It2, sentinel_for<_It2> _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();
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-07-22 7:07 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-22 7:07 [gcc r11-10163] libstdc++: Make std::common_iterator completely constexpr-able (LWG 3574) Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).