From 2e835288562cf49d34037a3c4c1c326009688a44 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 21 May 2019 12:46:54 +0200 Subject: [PATCH] QScopeGuard: some cleanups Use qExchange() in the move ctor and pass the function object by rvalue ref. This saves one move construction and doesn't produce unexpected results. The qScopeGuard free function should take the function object by value, because it decays and because we can't create an rvalure reference in a deduced context. But once we're inside qScopeGuard, the extra object isn't needed anymore, so optimize it away. Change-Id: I94cbc45f9bf6ca086e100efd922a0b4643a81671 Reviewed-by: Giuseppe D'Angelo --- src/corelib/tools/qscopeguard.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h index 41d0a6af68f..d20620e9337 100644 --- a/src/corelib/tools/qscopeguard.h +++ b/src/corelib/tools/qscopeguard.h @@ -60,9 +60,8 @@ QScopeGuard public: QScopeGuard(QScopeGuard &&other) noexcept : m_func(std::move(other.m_func)) - , m_invoke(other.m_invoke) + , m_invoke(qExchange(other.m_invoke, false)) { - other.dismiss(); } ~QScopeGuard() @@ -77,7 +76,7 @@ public: } private: - explicit QScopeGuard(F f) noexcept + explicit QScopeGuard(F &&f) noexcept : m_func(std::move(f)) { }