Disallow copy of SaveAndRestore (#7966)

An accidental copy would cause an extra save/restore. This is meant as
an RAII object to denote a save for a given scope. Also disallow move so
we don't need to add branches to the dtor.
This commit is contained in:
Dana Jansens authored and GitHub committed 2026-10-15 15:30:08 +00:00
1 parent 08b752e888
commit d649699682
1 file changed
+8 -3
+8 -3
View File
@@ -12,11 +12,12 @@
namespace Carbon {
// Sets a variable to a new value, and restores the original value when
// the SetAndRestore is destroyed. CHECKs that the variable is still holding the
// new value when restoring the original value, to catch any data loss.
// the CheckedSaveAndRestore is destroyed. CHECKs that the variable is still
// holding the new value when restoring the original value, to catch any data
// loss.
//
// The value must be copyable in order to compare with the new value at the time
// the SetAndRestore is destroyed.
// the CheckedSaveAndRestore is destroyed.
template <typename T>
class [[nodiscard]] CheckedSaveAndRestore {
public:
@@ -30,6 +31,10 @@ class [[nodiscard]] CheckedSaveAndRestore {
*var_ = std::move(orig_value_);
}
CheckedSaveAndRestore(const CheckedSaveAndRestore&) = delete;
auto operator=(const CheckedSaveAndRestore&)
-> CheckedSaveAndRestore& = delete;
private:
T* var_;
T new_value_;