Use std::shared_ptr for PromiseValue<T>::m_data instead of QSharedPoitner<T> (#60)

In optimized builds (such as -O2) GCC emits "maybe-uninitialized" warning for QSharedPointer<T>::d. This happens only for optimized builds because that is when GCC tracks lifetimes[1]. In a project with lots of QtPromise uses this produces a heap of bogus warnings. This warning has been previously reported to a Qt team, but they're confident it is a compiler bug[2][3]. However, this is a second time and issue raised with QSharedPointer and warnings to it. The first time it was addressed here: https://github.com/simonbrunel/qtpromise/pull/34.

[1] https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wmaybe-uninitialized
[2] https://bugreports.qt.io/browse/QTBUG-14637
[3] https://bugreports.qt.io/browse/QTBUG-77641
This commit is contained in:
Egor Krugletsov 2024-08-24 10:01:08 +03:00 committed by GitHub
parent 14031392ac
commit 1a905cbd4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,6 +20,8 @@
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtCore/QVector> #include <QtCore/QVector>
#include <memory>
namespace QtPromise { namespace QtPromise {
template<typename T> template<typename T>
@ -92,13 +94,13 @@ class PromiseValue
{ {
public: public:
PromiseValue() { } PromiseValue() { }
PromiseValue(const T& data) : m_data(QSharedPointer<T>::create(data)) { } PromiseValue(const T& data) : m_data(std::make_shared<T>(data)) { }
PromiseValue(T&& data) : m_data(QSharedPointer<T>::create(std::forward<T>(data))) { } PromiseValue(T&& data) : m_data(std::make_shared<T>(std::forward<T>(data))) { }
bool isNull() const { return m_data.isNull(); } bool isNull() const { return m_data == nullptr; }
const T& data() const { return *m_data; } const T& data() const { return *m_data; }
private: private:
QSharedPointer<T> m_data; std::shared_ptr<T> m_data;
}; };
class PromiseError class PromiseError