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.
This commit is contained in:
faluco 2021-09-13 15:06:57 +02:00 committed by faluco
parent 485ec2edab
commit cf20b143c0
1 changed files with 6 additions and 3 deletions

View File

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