From 4f077b7e5ff1081afc0e362bdab6522c2b7ee43b Mon Sep 17 00:00:00 2001 From: Kari Oikarinen Date: Mon, 11 Nov 2019 13:35:53 +0200 Subject: QScopeGuard: Make constructor public With Class Template Argument Deduction users might want to use the constructor itself instead of a separate helper function. In both cases it's possible to let the compiler deduce the template arguments. Try to make the usefulness of the helper function in the absence of CTAD still clear in the documentation. Change-Id: I9b07983c1fb276a6dd9e7ed4c3e606764e9b68ca Reviewed-by: Ville Voutilainen --- src/corelib/tools/qscopeguard.h | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/corelib/tools/qscopeguard.h') diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h index 40d2747b1d..4d2e715df1 100644 --- a/src/corelib/tools/qscopeguard.h +++ b/src/corelib/tools/qscopeguard.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sérgio Martins +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -45,10 +46,6 @@ QT_BEGIN_NAMESPACE - -template class QScopeGuard; -template QScopeGuard qScopeGuard(F f); - template class #if __has_cpp_attribute(nodiscard) @@ -59,13 +56,23 @@ Q_REQUIRED_RESULT QScopeGuard { public: + explicit QScopeGuard(F &&f) noexcept + : m_func(std::move(f)) + { + } + + explicit QScopeGuard(const F &f) noexcept + : m_func(f) + { + } + QScopeGuard(QScopeGuard &&other) noexcept : m_func(std::move(other.m_func)) , m_invoke(qExchange(other.m_invoke, false)) { } - ~QScopeGuard() + ~QScopeGuard() noexcept { if (m_invoke) m_func(); @@ -77,28 +84,34 @@ public: } private: - explicit QScopeGuard(F &&f) noexcept - : m_func(std::move(f)) - { - } - Q_DISABLE_COPY(QScopeGuard) F m_func; bool m_invoke = true; - friend QScopeGuard qScopeGuard(F); }; +#ifdef __cpp_deduction_guides +template QScopeGuard(F(&)()) -> QScopeGuard; +#endif template #if __has_cpp_attribute(nodiscard) Q_REQUIRED_RESULT #endif -QScopeGuard qScopeGuard(F f) +QScopeGuard 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); +} + QT_END_NAMESPACE #endif // QSCOPEGUARD_H -- cgit v1.2.3 From 89f443dfbc980313f19cc8c6a205491a41c9926a Mon Sep 17 00:00:00 2001 From: Kari Oikarinen Date: Thu, 30 Jan 2020 12:46:47 +0200 Subject: QScopeGuard: Fix build failures with qScopeGuard() Partially reverts 4f077b7e5ff1081afc0e362bdab6522c2b7ee43b. Can't overload with forwarding references and lvalue references. Use a single forwarding reference overload, but take care of not trying to create a QScopeGuard of reference type and forward instead of moving. Add tests to ensure calling with both lvalues and rvalues is possible. Change-Id: Ia034afe0a8feb08246c2c7c154a85cae37421c98 Reviewed-by: Ville Voutilainen Reviewed-by: Volker Hilsheimer Reviewed-by: Simon Hausmann --- src/corelib/tools/qscopeguard.h | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) (limited to 'src/corelib/tools/qscopeguard.h') diff --git a/src/corelib/tools/qscopeguard.h b/src/corelib/tools/qscopeguard.h index 4d2e715df1..6a5bc6cc61 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 -- cgit v1.2.3