[PATCH] libstdc++: Add noexcept to some std::counted_iterator operations

Jonathan Wakely jwakely@redhat.com
Fri May 2 11:01:59 GMT 2025


This was inspired by LWG 4245 but goes further. Anything which only
reads or writes the _M_length member can be noexcept. That
member is an iterator difference_type which means it's a signed integer
type or the __max_diff_type integer-like class type, so all arithmetic
and comparisons are non-throwing.

libstdc++-v3/ChangeLog:

	* include/bits/stl_iterator.h (counted_iterator): Add noexcept
	to friend operators which only access the _M_length member.
---

Tested x86_64-linux.

 libstdc++-v3/include/bits/stl_iterator.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index bed72955d0c..478a98fe8a4 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -2511,17 +2511,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	[[nodiscard]]
 	friend constexpr iter_difference_t<_It2>
 	operator-(const counted_iterator& __x,
-		  const counted_iterator<_It2>& __y)
+		  const counted_iterator<_It2>& __y) noexcept
 	{ return __y._M_length - __x._M_length; }
 
       [[nodiscard]]
       friend constexpr iter_difference_t<_It>
-      operator-(const counted_iterator& __x, default_sentinel_t)
+      operator-(const counted_iterator& __x, default_sentinel_t) noexcept
       { return -__x._M_length; }
 
       [[nodiscard]]
       friend constexpr iter_difference_t<_It>
-      operator-(default_sentinel_t, const counted_iterator& __y)
+      operator-(default_sentinel_t, const counted_iterator& __y) noexcept
       { return __y._M_length; }
 
       constexpr counted_iterator&
@@ -2548,19 +2548,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	[[nodiscard]]
 	friend constexpr bool
 	operator==(const counted_iterator& __x,
-		   const counted_iterator<_It2>& __y)
+		   const counted_iterator<_It2>& __y) noexcept
 	{ return __x._M_length == __y._M_length; }
 
       [[nodiscard]]
       friend constexpr bool
-      operator==(const counted_iterator& __x, default_sentinel_t)
+      operator==(const counted_iterator& __x, default_sentinel_t) noexcept
       { return __x._M_length == 0; }
 
       template<common_with<_It> _It2>
 	[[nodiscard]]
 	friend constexpr strong_ordering
 	operator<=>(const counted_iterator& __x,
-		    const counted_iterator<_It2>& __y)
+		    const counted_iterator<_It2>& __y) noexcept
 	{ return __y._M_length <=> __x._M_length; }
 
       [[nodiscard]]
-- 
2.49.0



More information about the Libstdc++ mailing list