[PATCH] D135940: [SaveAndRestore] Upgrade this to support non-copyable types.

Chris Lattner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 13 22:01:32 PDT 2022


lattner created this revision.
Herald added a project: All.
lattner requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This adds a constructor and upgrades the dtor to work with
move-only types.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135940

Files:
  llvm/include/llvm/Support/SaveAndRestore.h


Index: llvm/include/llvm/Support/SaveAndRestore.h
===================================================================
--- llvm/include/llvm/Support/SaveAndRestore.h
+++ llvm/include/llvm/Support/SaveAndRestore.h
@@ -20,11 +20,12 @@
 /// A utility class that uses RAII to save and restore the value of a variable.
 template <typename T> struct SaveAndRestore {
   SaveAndRestore(T &X) : X(X), OldValue(X) {}
-  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) {
-    X = NewValue;
+  SaveAndRestore(T &X, const T &NewValue) : X(X), OldValue(X) { X = NewValue; }
+  SaveAndRestore(T &X, T &&NewValue) : X(X), OldValue(std::move(X)) {
+    X = std::move(NewValue);
   }
-  ~SaveAndRestore() { X = OldValue; }
-  T get() { return OldValue; }
+  ~SaveAndRestore() { X = std::move(OldValue); }
+  const T &get() { return OldValue; }
 
 private:
   T &X;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135940.467682.patch
Type: text/x-patch
Size: 859 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221014/0335f000/attachment.bin>


More information about the llvm-commits mailing list