summaryrefslogtreecommitdiffstats
path: root/src/corelib/global
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-03-31 14:22:02 +0200
committerMarc Mutz <marc.mutz@qt.io>2022-04-01 23:03:58 +0200
commitd9531593a248e19f7da7862b2870a6af2f413e75 (patch)
tree5260582517ee4f573621c1333695dc31b96357f2 /src/corelib/global
parent0e3ac20088d2b07a767e5379ffc68ad29e2e0150 (diff)
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 <giuseppe.dangelo@kdab.com> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/corelib/global')
-rw-r--r--src/corelib/global/qglobal.h8
1 files changed, 8 insertions, 0 deletions
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<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(); }