Fix the default constructor of the optional class to avoid a spurious uninitialized value warning in older GCC versions (seen for ARM32 and GCC 8.3).

Use a union of a byte and the real storage, and init the char by default.
master
faluco 3 years ago committed by faluco
parent 485ec2edab
commit cf20b143c0

@ -24,7 +24,7 @@ class optional
public: public:
using value_type = T; using value_type = T;
optional() : has_val_(false) {} optional() : has_val_(false), empty() {}
optional(const T& t) : has_val_(true) { storage.emplace(t); } optional(const T& t) : has_val_(true) { storage.emplace(t); }
optional(T&& t) : has_val_(true) { storage.emplace(std::move(t)); } optional(T&& t) : has_val_(true) { storage.emplace(std::move(t)); }
optional(const optional<T>& other) : has_val_(other.has_value()) optional(const optional<T>& other) : has_val_(other.has_value())
@ -99,8 +99,11 @@ public:
private: private:
bool has_val_; bool has_val_;
union {
char empty;
detail::type_storage<T> storage; detail::type_storage<T> storage;
}; };
};
template <typename T> template <typename T>
bool operator==(const optional<T>& lhs, const optional<T>& rhs) bool operator==(const optional<T>& lhs, const optional<T>& rhs)

Loading…
Cancel
Save