diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h index 4d2e715df12..6a5bc6cc61e 100644 --- a/src/corelib/tools/qscopeguard.h +++ b/src/corelib/tools/qscopeguard.h @@ -43,6 +43,8 @@ #include +#include +#include QT_BEGIN_NAMESPACE @@ -98,18 +100,9 @@ template #if __has_cpp_attribute(nodiscard) Q_REQUIRED_RESULT #endif -QScopeGuard qScopeGuard(F &&f) +QScopeGuard::type> qScopeGuard(F &&f) { - return QScopeGuard(std::move(f)); -} - -template -#if __has_cpp_attribute(nodiscard) -Q_REQUIRED_RESULT -#endif -QScopeGuard qScopeGuard(const F &f) -{ - return QScopeGuard(f); + return QScopeGuard::type>(std::forward(f)); } QT_END_NAMESPACE diff --git a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp index 24c30231831..e5393f694e8 100644 --- a/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp +++ b/tests/auto/corelib/tools/qscopeguard/tst_qscopeguard.cpp @@ -112,12 +112,19 @@ void tst_QScopeGuard::constructionFromLvalue() { #ifdef __cpp_deduction_guides Callable::resetCounts(); - Callable callable; { + Callable callable; QScopeGuard guard(callable); } QCOMPARE(Callable::copied, 1); QCOMPARE(Callable::moved, 0); + Callable::resetCounts(); + { + Callable callable; + auto guard = qScopeGuard(callable); + } + QCOMPARE(Callable::copied, 1); + QCOMPARE(Callable::moved, 0); #else QSKIP("This test requires C++17 Class Template Argument Deduction support enabled in the compiler."); #endif @@ -133,6 +140,13 @@ void tst_QScopeGuard::constructionFromRvalue() } QCOMPARE(Callable::copied, 0); QCOMPARE(Callable::moved, 1); + Callable::resetCounts(); + { + Callable callable; + auto guard = qScopeGuard(std::move(callable)); + } + QCOMPARE(Callable::copied, 0); + QCOMPARE(Callable::moved, 1); #else QSKIP("This test requires C++17 Class Template Argument Deduction support enabled in the compiler."); #endif