Skip to content

Conversation

@philnik777
Copy link
Contributor

Fixes #116204

@philnik777 philnik777 requested a review from a team as a code owner November 14, 2024 22:34
__other.__moved_from_ = true;
}
private:
__scope_guard(__scope_guard&&);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this approach is better than having dead code. @ldionne thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I like this a lot better than the previous approach. There's not much harm in providing this private declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh... this actually has to be public. Sometimes my brain just turns off. Does that change your thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't, but thanks for checking.

@github-actions
Copy link

github-actions bot commented Nov 14, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

__other.__moved_from_ = true;
}
private:
__scope_guard(__scope_guard&&);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I like this a lot better than the previous approach. There's not much harm in providing this private declaration.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 16, 2024

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

Fixes #116204


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

1 Files Affected:

  • (modified) libcxx/include/__utility/scope_guard.h (+7-11)
diff --git a/libcxx/include/__utility/scope_guard.h b/libcxx/include/__utility/scope_guard.h
index 133e54212ed592..e51b300d1f50c7 100644
--- a/libcxx/include/__utility/scope_guard.h
+++ b/libcxx/include/__utility/scope_guard.h
@@ -26,26 +26,22 @@ _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;
-  }
+  __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>

@philnik777 philnik777 merged commit adb80d8 into llvm:main Nov 16, 2024
58 of 62 checks passed
@philnik777 philnik777 deleted the fix_scope_guard branch November 16, 2024 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[libc++] __scope_guard's constructor should be explicit

3 participants