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 32692667a6.

Change-Id: Ic2247768b0d64e0c01648cffc9532fe5bd4bbd5d
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
bb10
Marc Mutz 2022-03-31 14:22:02 +02:00
parent 0e3ac20088
commit d9531593a2
2 changed files with 9 additions and 1 deletions

View File

@ -1167,6 +1167,10 @@ constexpr std::underlying_type_t<Enum> 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<Enum> qToUnderlying(Enum e) noexcept
# define Q_CONSTINIT
#endif
#ifndef Q_THREAD_LOCAL_CONSTINIT
# define Q_THREAD_LOCAL_CONSTINIT Q_CONSTINIT
#endif
template <typename T> inline T *qGetPtrHelper(T *ptr) noexcept { return ptr; }
template <typename Ptr> 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(); }

View File

@ -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