From d9531593a248e19f7da7862b2870a6af2f413e75 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 31 Mar 2022 14:22:02 +0200 Subject: [PATCH] Add Q_THREAD_LOCAL_CONSTINIT to work around an MSVC bug MSVC 19 does not allow using constinit on thread_local objects of non-trivial type: https://developercommunity.visualstudio.com/t/C:-constinit-for-an-optional-fails-if-/1406069 Instead of revoking Q_CONSTINIT for that compiler or removing Q_CONSTINIT from such variables, add a new macro for this situation, so the constinit static assertion will still be checked on other platforms. Amends 32692667a625f31aa02e0303f2139c780ae42694. Change-Id: Ic2247768b0d64e0c01648cffc9532fe5bd4bbd5d Reviewed-by: Giuseppe D'Angelo Reviewed-by: Allan Sandfeld Jensen --- src/corelib/global/qglobal.h | 8 ++++++++ src/corelib/kernel/qproperty.cpp | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 18851cd120..bc13d516e6 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1167,6 +1167,10 @@ constexpr std::underlying_type_t qToUnderlying(Enum e) noexcept #ifdef __cpp_constinit # define Q_CONSTINIT constinit +# if defined(Q_CC_MSVC) && !defined(Q_CC_CLANG) + // https://developercommunity.visualstudio.com/t/C:-constinit-for-an-optional-fails-if-/1406069 +# define Q_THREAD_LOCAL_CONSTINIT +# endif #elif defined(__has_cpp_attribute) && __has_cpp_attribute(clang::require_constant_initialization) # define Q_CONSTINIT [[clang::require_constant_initialization]] #elif defined(Q_CC_GNU) && Q_CC_GNU >= 1000 @@ -1175,6 +1179,10 @@ constexpr std::underlying_type_t qToUnderlying(Enum e) noexcept # define Q_CONSTINIT #endif +#ifndef Q_THREAD_LOCAL_CONSTINIT +# define Q_THREAD_LOCAL_CONSTINIT Q_CONSTINIT +#endif + template inline T *qGetPtrHelper(T *ptr) noexcept { return ptr; } template inline auto qGetPtrHelper(Ptr &ptr) noexcept -> decltype(ptr.get()) { static_assert(noexcept(ptr.get()), "Smart d pointers for Q_DECLARE_PRIVATE must have noexcept get()"); return ptr.get(); } diff --git a/src/corelib/kernel/qproperty.cpp b/src/corelib/kernel/qproperty.cpp index 297a672d6a..67c55e56aa 100644 --- a/src/corelib/kernel/qproperty.cpp +++ b/src/corelib/kernel/qproperty.cpp @@ -198,7 +198,7 @@ struct QPropertyDelayedNotifications } }; -Q_CONSTINIT static thread_local QBindingStatus bindingStatus; +Q_THREAD_LOCAL_CONSTINIT static thread_local QBindingStatus bindingStatus; /*! \since 6.2