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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Oct 13 01:56:47 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

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

As a drive-by this also simplifies the brace positioning.

---
Full diff: https://github.com/llvm/llvm-project/pull/230988.diff


4 Files Affected:

- (modified) libcxx/include/__flat_map/flat_map.h (+6-10) 
- (modified) libcxx/include/__flat_map/flat_multimap.h (+6-10) 
- (modified) libcxx/include/__flat_set/flat_multiset.h (+6-9) 
- (modified) libcxx/include/__flat_set/flat_set.h (+6-9) 


``````````diff
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)

``````````

</details>


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


More information about the libcxx-commits mailing list