[libcxx-commits] [libcxx] [libc++] Add the __is_replaceable type trait (PR #132408)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Mar 25 09:21:10 PDT 2025


================
@@ -65,8 +65,11 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT;
 
 public:
-  // exception_ptr is basically a COW string.
+  // exception_ptr is basically a COW string so it is trivially relocatable.
+  // However, it's not replaceable because destroying and move-constructing could cause
+  // the underlying refcount to hit 0 if we're self-assigning.
----------------
ldionne wrote:

The semantics of `__is_replaceable` mean that move-assignment is equivalent to destroy followed by a move construction. However, you can't replace a move-assignment by a destroy + move-construct for `std::exception_ptr`, since if both the source and destination are the same object, the destroy + move-construction will result in the refcount hitting 0 (after destruction) whereas the move-assignment wouldn't have.

This actually makes me realize: you can never do destroy + move-construct if the source and the destination are the same object. This has nothing to do with the presence of a refcount in the object. How is `__is_replaceable` supposed to work in that case? Aren't *all* types non-replaceable, then?

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


More information about the libcxx-commits mailing list