qcompilerdetection.h: Introduce Q_DECL_CONSTEXPR_DTOR macro

Add this macro to be able to declare explicit destructors constexpr.
This became possible starting from C++20, where constexpr >= 201907L.

Introduce a syntax test in test_qglobal.cpp.

Document the new macro.

Replace "#if __cpp_constexpr >= 201907L constexpr" with the macro
(QScopedValueRollback has this).

[ChangeLog][QtCore] Introduced Q_DECL_CONSTEXPR_DTOR which
resolves to constexpr when __cpp_constexpr >= 201907L, otherwise
it resolves to inline.

Task-number: QTBUG-124462
Change-Id: I763ce1e4674fdc6d7307a63df9a45cc4db253fc4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Rym Bouabid 2024-06-19 15:17:52 +02:00
parent 384acdc1cf
commit 3ab18f4021
4 changed files with 44 additions and 5 deletions

View File

@ -514,6 +514,8 @@
*
* For a full listing of feature test macros, see
* https://en.cppreference.com/w/cpp/feature_test
* Exceptions:
* Q_DECL_CONSTEXPR_DTOR constexpr in C++20 for explicit destructors __cpp_constexpr >= 201907L
*
* C++ extensions:
* Q_COMPILER_RESTRICTED_VLA variable-length arrays, prior to __cpp_runtime_arrays
@ -1003,6 +1005,14 @@
#define Q_DECL_ENUMERATOR_DEPRECATED Q_DECL_DEPRECATED
#define Q_DECL_ENUMERATOR_DEPRECATED_X(x) Q_DECL_DEPRECATED_X(x)
#ifndef Q_DECL_CONSTEXPR_DTOR
# if __cpp_constexpr >= 201907L
# define Q_DECL_CONSTEXPR_DTOR constexpr
# else
# define Q_DECL_CONSTEXPR_DTOR inline
# endif
#endif
/*
* Fallback macros to certain compiler features
*/

View File

@ -275,7 +275,7 @@
This macro can be used to declare variable that should be constructed at compile-time,
or an inline function that can be computed at compile-time.
\sa Q_DECL_RELAXED_CONSTEXPR
\sa Q_DECL_RELAXED_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
*/
/*!
@ -286,7 +286,20 @@
This macro can be used to declare an inline function that can be computed
at compile-time according to the relaxed rules from C++14.
\sa Q_DECL_CONSTEXPR
\sa Q_DECL_CONSTEXPR, Q_DECL_CONSTEXPR_DTOR
*/
/*!
\macro Q_DECL_CONSTEXPR_DTOR
\relates <QtCompilerDetection>
\since 6.9
Expands to \c{constexpr} on compilers that support C++20, where
\c{__cpp_constexpr} has a value greater than or equal to \c{201907L}.
Otherwise, it expands to \c{inline}.
Use this macro to declare a destructor that can be computed at
compile-time in C++20 or later.
*/
/*!

View File

@ -25,9 +25,7 @@ public:
var = std::move(value);
}
#if __cpp_constexpr >= 201907L
constexpr
#endif
Q_DECL_CONSTEXPR_DTOR
~QScopedValueRollback()
{
varRef = std::move(oldValue);

View File

@ -97,6 +97,7 @@ private slots:
void testqToUnderlying();
void nodiscard();
void tagStructDefinitions();
void CXX20_constexpr_dtor();
};
extern "C" { // functions in qglobal.c
@ -995,6 +996,23 @@ void tst_QGlobal::tagStructDefinitions()
}
}
class DestructorTest {
public:
explicit constexpr DestructorTest(int *arr) : m_arr(arr) {}
Q_DECL_CONSTEXPR_DTOR ~DestructorTest() { delete[] m_arr; }
private:
int *m_arr;
};
[[maybe_unused]] Q_DECL_CONSTEXPR_DTOR DestructorTest dt(nullptr);
void tst_QGlobal::CXX20_constexpr_dtor()
{
#if __cpp_constexpr >= 201907L
[[maybe_unused]] constexpr DestructorTest tmp(nullptr);
#endif
}
QT_BEGIN_NAMESPACE
// Compile-time typeinfo tests