summaryrefslogtreecommitdiff
path: root/src/corelib/tools/qscopeguard.h
diff options
context:
space:
mode:
authorKari Oikarinen <kari.oikarinen@qt.io>2019-11-11 13:35:53 +0200
committerKari Oikarinen <kari.oikarinen@qt.io>2020-01-30 07:30:17 +0200
commit4f077b7e5ff1081afc0e362bdab6522c2b7ee43b (patch)
tree84539da88bb36b09417fbbfb30c1175a21d18813 /src/corelib/tools/qscopeguard.h
parent601ce9e08aa92b273f1a6daf0bdbc67dbf9b4e5f (diff)
downloadqtbase-4f077b7e5ff1081afc0e362bdab6522c2b7ee43b.tar.gz
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 <ville.voutilainen@qt.io>
Diffstat (limited to 'src/corelib/tools/qscopeguard.h')
-rw-r--r--src/corelib/tools/qscopeguard.h37
1 files changed, 25 insertions, 12 deletions
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 <sergio.martins@kdab.com>
+** 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 <typename F> class QScopeGuard;
-template <typename F> QScopeGuard<F> qScopeGuard(F f);
-
template <typename F>
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>(F);
};
+#ifdef __cpp_deduction_guides
+template <typename F> QScopeGuard(F(&)()) -> QScopeGuard<F(*)()>;
+#endif
template <typename F>
#if __has_cpp_attribute(nodiscard)
Q_REQUIRED_RESULT
#endif
-QScopeGuard<F> qScopeGuard(F f)
+QScopeGuard<F> qScopeGuard(F &&f)
{
return QScopeGuard<F>(std::move(f));
}
+template <typename F>
+#if __has_cpp_attribute(nodiscard)
+Q_REQUIRED_RESULT
+#endif
+QScopeGuard<F> qScopeGuard(const F &f)
+{
+ return QScopeGuard<F>(f);
+}
+
QT_END_NAMESPACE
#endif // QSCOPEGUARD_H