[libcxx-commits] [libcxx] [libc++] Don't explicitly rethrow in flat container function try blocks (PR #230988)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 11 08:18:19 PDT 2026


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/230988

We currently have some rather nasty code in the flat containers' function catch blocks to work around a GCC warning. Instead, we can let the rethrow happen implicitly.


>From 4e16bcc590b211a336733801b6883d890b7acbe1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 11 Oct 2026 17:15:25 +0200
Subject: [PATCH] [libc++] Don't explicitly rethrow in flat container function
 try blocks

---
 libcxx/include/__flat_map/flat_map.h      | 16 ++++++----------
 libcxx/include/__flat_map/flat_multimap.h | 16 ++++++----------
 libcxx/include/__flat_set/flat_multiset.h | 15 ++++++---------
 libcxx/include/__flat_set/flat_set.h      | 15 ++++++---------
 4 files changed, 24 insertions(+), 38 deletions(-)

diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h
index 27964aa421d21..fb724fa3a16ee 100644
--- a/libcxx/include/__flat_map/flat_map.h
+++ b/libcxx/include/__flat_map/flat_map.h
@@ -154,16 +154,12 @@ class flat_map {
 #  endif // _LIBCPP_HAS_EXCEPTIONS
       : __containers_(std::move(__other.__containers_)), __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    // gcc does not like the `throw` keyword in a conditionally noexcept function
-    if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> &&
-                    is_nothrow_move_constructible_v<_MappedContainer> && is_nothrow_move_constructible_v<_Compare>)) {
-      throw;
-    }
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   template <class _Allocator>
     requires __allocator_ctor_constraint<_Allocator>
@@ -186,12 +182,12 @@ class flat_map {
                  std::move(__other.__containers_.values),
                  std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    throw;
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(
       key_container_type __key_cont, mapped_container_type __mapped_cont, const key_compare& __comp = key_compare())
diff --git a/libcxx/include/__flat_map/flat_multimap.h b/libcxx/include/__flat_map/flat_multimap.h
index 9acaedf0772a0..677560660a6de 100644
--- a/libcxx/include/__flat_map/flat_multimap.h
+++ b/libcxx/include/__flat_map/flat_multimap.h
@@ -154,16 +154,12 @@ class flat_multimap {
 #  endif // _LIBCPP_HAS_EXCEPTIONS
       : __containers_(std::move(__other.__containers_)), __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    // gcc does not like the `throw` keyword in a conditionally noexcept function
-    if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> &&
-                    is_nothrow_move_constructible_v<_MappedContainer> && is_nothrow_move_constructible_v<_Compare>)) {
-      throw;
-    }
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   template <class _Allocator>
     requires __allocator_ctor_constraint<_Allocator>
@@ -187,12 +183,12 @@ class flat_multimap {
                       std::move(__other.__containers_.values),
                       std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    throw;
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_multimap(
       key_container_type __key_cont, mapped_container_type __mapped_cont, const key_compare& __comp = key_compare())
diff --git a/libcxx/include/__flat_set/flat_multiset.h b/libcxx/include/__flat_set/flat_multiset.h
index 74c0427eae928..d549f7ab78f1b 100644
--- a/libcxx/include/__flat_set/flat_multiset.h
+++ b/libcxx/include/__flat_set/flat_multiset.h
@@ -112,15 +112,12 @@ class flat_multiset {
 #  endif // _LIBCPP_HAS_EXCEPTIONS
       : __keys_(std::move(__other.__keys_)), __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    // gcc does not like the `throw` keyword in a conditionally noexcept function
-    if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)) {
-      throw;
-    }
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_multiset(const key_compare& __comp)
       : __keys_(), __compare_(__comp) {}
@@ -231,12 +228,12 @@ class flat_multiset {
       : __keys_(std::make_obj_using_allocator<container_type>(__alloc, std::move(__other.__keys_))),
         __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    throw;
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   template <class _InputIterator, class _Allocator>
     requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)
diff --git a/libcxx/include/__flat_set/flat_set.h b/libcxx/include/__flat_set/flat_set.h
index 988ffbe1b4a70..63f3b6519cb6a 100644
--- a/libcxx/include/__flat_set/flat_set.h
+++ b/libcxx/include/__flat_set/flat_set.h
@@ -113,15 +113,12 @@ class flat_set {
 #  endif // _LIBCPP_HAS_EXCEPTIONS
       : __keys_(std::move(__other.__keys_)), __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    // gcc does not like the `throw` keyword in a conditionally noexcept function
-    if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)) {
-      throw;
-    }
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_set(const key_compare& __comp)
       : __keys_(), __compare_(__comp) {}
@@ -232,12 +229,12 @@ class flat_set {
       : __keys_(std::make_obj_using_allocator<container_type>(__alloc, std::move(__other.__keys_))),
         __compare_(std::move(__other.__compare_)) {
     __other.clear();
+  }
 #  if _LIBCPP_HAS_EXCEPTIONS
-  } catch (...) {
+  catch (...) {
     __other.clear();
-    throw;
-#  endif // _LIBCPP_HAS_EXCEPTIONS
   }
+#  endif // _LIBCPP_HAS_EXCEPTIONS
 
   template <class _InputIterator, class _Allocator>
     requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)



More information about the libcxx-commits mailing list