[libcxx-commits] [libcxx] [libc++] Address post-commit comments for __scope_guard (PR #116291)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 14 14:34:30 PST 2024


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

Fixes #116204


>From a91e0de7c03d6609418982a4849f4d25926aecf8 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 14 Nov 2024 23:33:43 +0100
Subject: [PATCH] [libc++] Address post-commit comments for __scope_guard

---
 libcxx/include/__utility/scope_guard.h | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/libcxx/include/__utility/scope_guard.h b/libcxx/include/__utility/scope_guard.h
index 133e54212ed592..d149729fa7cc97 100644
--- a/libcxx/include/__utility/scope_guard.h
+++ b/libcxx/include/__utility/scope_guard.h
@@ -26,26 +26,23 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Func>
 class __scope_guard {
   _Func __func_;
-  bool __moved_from_;
 
 public:
-  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __scope_guard(_Func __func) : __func_(std::move(__func)) {}
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit __scope_guard(_Func __func) : __func_(std::move(__func)) {}
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__scope_guard() { __func_(); }
 
-  __scope_guard(const __scope_guard&) = delete;
+  __scope_guard(const __scope_guard&)            = delete;
+  __scope_guard& operator=(const __scope_guard&) = delete;
+  __scope_guard& operator=(__scope_guard&&)      = delete;
 
-// C++17 has mandatory RVO, so we don't need the move constructor anymore to make __make_scope_guard work.
+// C++14 doesn't have mandatory RVO, so we have to provide a declaration even though no compiler will ever generate
+// a call to the move constructor.
 #if _LIBCPP_STD_VER <= 14
-  __scope_guard(__scope_guard&& __other) : __func_(__other.__func_) {
-    _LIBCPP_ASSERT_INTERNAL(!__other.__moved_from_, "Cannot move twice from __scope_guard");
-    __other.__moved_from_ = true;
-  }
+private:
+  __scope_guard(__scope_guard&&);
 #else
   __scope_guard(__scope_guard&&) = delete;
 #endif
-
-  __scope_guard& operator=(const __scope_guard&) = delete;
-  __scope_guard& operator=(__scope_guard&&)      = delete;
 };
 
 template <class _Func>



More information about the libcxx-commits mailing list