diff --git a/src/corelib/thread/qthreadstorage.h b/src/corelib/thread/qthreadstorage.h index 35ba0700796..32dc29f0c26 100644 --- a/src/corelib/thread/qthreadstorage.h +++ b/src/corelib/thread/qthreadstorage.h @@ -14,6 +14,18 @@ QT_BEGIN_NAMESPACE #if QT_CONFIG(thread) +template struct QThreadStorageTraits +{ + static constexpr void warnAboutTrivial() {} +}; +template <> struct QThreadStorageTraits +{ +#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING + Q_DECL_DEPRECATED_X("QThreadStorage used with a trivial non-pointer type; consider using thread_local") +#endif + static constexpr void warnAboutTrivial() noexcept {} +}; + class Q_CORE_EXPORT QThreadStorageData { public: @@ -87,6 +99,7 @@ template class QThreadStorage { private: + using Trait = QThreadStorageTraits && !std::is_pointer_v>; QThreadStorageData d; Q_DISABLE_COPY(QThreadStorage) @@ -95,7 +108,7 @@ private: { qThreadStorage_deleteData(x, reinterpret_cast(0)); } public: - inline QThreadStorage() : d(deleteData) { } + inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); } inline ~QThreadStorage() { } inline bool hasLocalData() const diff --git a/tests/auto/corelib/thread/qthreadstorage/crashonexit/crashOnExit.cpp b/tests/auto/corelib/thread/qthreadstorage/crashonexit/crashOnExit.cpp index 3b3a4d4813c..52dc5e22ac7 100644 --- a/tests/auto/corelib/thread/qthreadstorage/crashonexit/crashOnExit.cpp +++ b/tests/auto/corelib/thread/qthreadstorage/crashonexit/crashOnExit.cpp @@ -1,6 +1,7 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only +#define Q_NO_THREAD_STORAGE_TRIVIAL_WARNING #include #include diff --git a/tests/auto/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp b/tests/auto/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp index 0449909c889..e3734d27f7f 100644 --- a/tests/auto/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp +++ b/tests/auto/corelib/thread/qthreadstorage/tst_qthreadstorage.cpp @@ -1,6 +1,8 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only +#define Q_NO_THREAD_STORAGE_TRIVIAL_WARNING + #include #if QT_CONFIG(process) #include