/// 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;