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 <giuseppe.dangelo@kdab.com>
This commit is contained in:
Marc Mutz 2019-05-21 12:46:54 +02:00
parent 4b09d5a78d
commit 2e83528856

View File

@ -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))
{
}