const None none = nullptr;
-/**
- * gcc-4.7 warns about use of uninitialized memory around the use of storage_
- * even though this is explicitly initialized at each point.
- */
-#if defined(__GNUC__) && !defined(__clang__)
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wuninitialized"
-# pragma GCC diagnostic ignored "-Wpragmas"
-# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
-#endif // __GNUC__
-
class OptionalEmptyException : public std::runtime_error {
public:
OptionalEmptyException()
}
struct StorageTriviallyDestructible {
- // uninitialized
- union { Value value; };
- bool hasValue;
+ // The union trick allows to initialize the Optional's memory,
+ // so that compiler/tools don't complain about unitialized memory,
+ // without actually calling Value's default constructor.
+ // The rest of the implementation enforces that hasValue/value are
+ // synchronized.
+ union {
+ bool hasValue;
+ struct {
+ bool paddingForHasValue_[1];
+ Value value;
+ };
+ };
StorageTriviallyDestructible() : hasValue{false} {}
};
struct StorageNonTriviallyDestructible {
- // uninitialized
- union { Value value; };
- bool hasValue;
+ // See StorageTriviallyDestructible's union
+ union {
+ bool hasValue;
+ struct {
+ bool paddingForHasValue_[1];
+ Value value;
+ };
+ };
StorageNonTriviallyDestructible() : hasValue{false} {}
-
~StorageNonTriviallyDestructible() {
clear();
}
Storage storage_;
};
-#if defined(__GNUC__) && !defined(__clang__)
-#pragma GCC diagnostic pop
-#endif
-
template<class T>
const T* get_pointer(const Optional<T>& opt) {
return opt.get_pointer();