[libcxx-commits] [PATCH] D81954: Remove the try/catch codepath if swap is noexcept.

Michael Park via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 16 11:00:14 PDT 2020


mpark created this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Fixes https://bugs.llvm.org/show_bug.cgi?id=46342


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81954

Files:
  libcxx/include/variant


Index: libcxx/include/variant
===================================================================
--- libcxx/include/variant
+++ libcxx/include/variant
@@ -1062,21 +1062,28 @@
         _VSTD::swap(__lhs, __rhs);
       }
       __impl __tmp(_VSTD::move(*__rhs));
-#ifndef _LIBCPP_NO_EXCEPTIONS
-      // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
-      // and `__tmp` is nothrow move constructible then we move `__tmp` back
-      // into `__rhs` and provide the strong exception safety guarantee.
-      try {
+      static constexpr bool is_noexcept =
+#ifdef _LIBCPP_NO_EXCEPTIONS
+          true;
+#else
+          __all<(is_nothrow_move_constructible_v<_Types> &&
+                 is_nothrow_swappable_v<_Types>)...>::value;
+#endif
+      if constexpr (is_noexcept) {
         this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
-      } catch (...) {
-        if (__tmp.__move_nothrow()) {
-          this->__generic_construct(*__rhs, _VSTD::move(__tmp));
+      } else {
+        // EXTENSION: When the move construction of `__lhs` into `__rhs` throws
+        // and `__tmp` is nothrow move constructible then we move `__tmp` back
+        // into `__rhs` and provide the strong exception safety guarantee.
+        try {
+          this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
+        } catch (...) {
+          if (__tmp.__move_nothrow()) {
+            this->__generic_construct(*__rhs, _VSTD::move(__tmp));
+          }
+          throw;
         }
-        throw;
       }
-#else
-      this->__generic_construct(*__rhs, _VSTD::move(*__lhs));
-#endif
       this->__generic_construct(*__lhs, _VSTD::move(__tmp));
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81954.271141.patch
Type: text/x-patch
Size: 1683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200616/4496fb4b/attachment.bin>


More information about the libcxx-commits mailing list