mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-10 12:28:25 +00:00
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:
1 parent
08b752e888
commit
d649699682
1 file changed
+8
-3
@@ -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_;
|
||||
|
||||
Reference in new issue
Block a user