From 8fa8c574c04ea4f82d7675d5be5b9808c0b8f7b6 Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Thu, 11 Jul 2024 12:53:38 +0200 Subject: [PATCH] qcompilerdetection.h: Introduce Q_CONSTEXPR_DTOR macro for variables Add this macro to declare constexpr variables that have destructors marked with Q_DECL_CONSTEXPR_DTOR. Declare variables using the new Q_CONSTEXPR_DTOR macro instead of Q_DECL_CONSTEXPR_DTOR in syntax tests. Move the declarations inside the functions as Q_DECL_CONSTEXPR_DTOR could only be used to declare non-local variables. [ChangeLog][QtCore] Introduced Q_CONSTEXPR_DTOR for variables, which resolves to constexpr when __cpp_constexpr >= 201907L, otherwise it resolves to const. Task-number: QTBUG-124462 Change-Id: I06cd99bb956d9af560e92c326a7c82aa4c8852d1 Reviewed-by: Thiago Macieira --- src/corelib/global/qcompilerdetection.h | 9 +++++++++ src/corelib/global/qcompilerdetection.qdoc | 17 +++++++++++++++++ .../auto/corelib/global/qglobal/tst_qglobal.cpp | 3 +-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h index dae47bf7a4e..c148a66bd19 100644 --- a/src/corelib/global/qcompilerdetection.h +++ b/src/corelib/global/qcompilerdetection.h @@ -516,6 +516,7 @@ * https://en.cppreference.com/w/cpp/feature_test * Exceptions: * Q_DECL_CONSTEXPR_DTOR constexpr in C++20 for explicit destructors __cpp_constexpr >= 201907L + * Q_CONSTEXPR_DTOR constexpr in C++20 for variables __cpp_constexpr >= 201907L otherwise const * Q_DECL_EQ_DELETE_X(message) = delete("reason"), __cpp_deleted_function >= 202403L * * C++ extensions: @@ -1014,6 +1015,14 @@ # endif #endif +#ifndef Q_CONSTEXPR_DTOR +# if __cpp_constexpr >= 201907L +# define Q_CONSTEXPR_DTOR constexpr +# else +# define Q_CONSTEXPR_DTOR const +# endif +#endif + #ifndef Q_DECL_EQ_DELETE_X # if defined(__cpp_deleted_function) && __cpp_deleted_function >= 202403L # define Q_DECL_EQ_DELETE_X(reason) = delete(reason) diff --git a/src/corelib/global/qcompilerdetection.qdoc b/src/corelib/global/qcompilerdetection.qdoc index 9b5330e14d9..114f2507d16 100644 --- a/src/corelib/global/qcompilerdetection.qdoc +++ b/src/corelib/global/qcompilerdetection.qdoc @@ -300,6 +300,23 @@ Use this macro to declare a destructor that can be computed at compile-time in C++20 or later. + + \sa Q_CONSTEXPR_DTOR +*/ + +/*! + \macro Q_CONSTEXPR_DTOR + \relates + \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{const}. + + Use this macro to declare a variable that can be constructed at compile-time + in C++20 or later. + + \sa Q_DECL_CONSTEXPR_DTOR */ /*! diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index de593653ebf..963ae09ca2b 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -1004,10 +1004,9 @@ private: int *m_arr; }; -[[maybe_unused]] Q_DECL_CONSTEXPR_DTOR DestructorTest dt(nullptr); - void tst_QGlobal::CXX20_constexpr_dtor() { + [[maybe_unused]] Q_CONSTEXPR_DTOR DestructorTest tmp0(nullptr); #if __cpp_constexpr >= 201907L [[maybe_unused]] constexpr DestructorTest tmp(nullptr); #endif