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