From 64637c599d04593621481aeec9899331c5af85b8 Mon Sep 17 00:00:00 2001 From: Felix Chern Date: Thu, 9 Jan 2025 07:48:03 -0800 Subject: [PATCH] Remove move ctor/assignment from TriStatePtr::SharedRef Moving TriStatePtr::SharedRef is error prone to get the refcount right. While it is possible to implement one, the use case is so rare to justify. PiperOrigin-RevId: 713670111 --- cpp/tri_state_ptr.h | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/cpp/tri_state_ptr.h b/cpp/tri_state_ptr.h index 197e129..ce10006 100644 --- a/cpp/tri_state_ptr.h +++ b/cpp/tri_state_ptr.h @@ -84,7 +84,6 @@ class TriStatePtr { &state_)); } - // explicit TriStatePtr(std::unique_ptr ptr) : ptr_(std::move(ptr)) {} explicit TriStatePtr(std::unique_ptr ptr) : ptr_(std::move(ptr)) {} class SharedRef { @@ -95,24 +94,12 @@ class TriStatePtr { parent_->ref_count_++; } SharedRef& operator=(const SharedRef& other) { - this->parent_ = other.parent_; - this->parent_->ref_count_++; - return *this; - } - - SharedRef(SharedRef&& other) : parent_(other.parent_) { - other.parent_ = nullptr; - } - SharedRef& operator=(SharedRef&& other) { - this->parent_ = other.parent_; - other.parent_ = nullptr; + parent_ = other.parent_; + parent_->ref_count_++; return *this; } ~SharedRef() { - if (parent_ == nullptr) { - return; - } int32_t ref_count = parent_->ref_count_.fetch_sub(1, std::memory_order_acq_rel) - 1; if (ref_count == 0) {