Fix base::Optional constexpr ctor on gcc 4.8.
g++ 4.8 failed to compile base::Optional<std::unique_ptr<int>>().
A simpler example is:
class C {
public:
constexpr C() {}
~C() {}
private:
union {
char empty_ = '\0';
std::unique_ptr<int> value_;
};
};
g++ 4.8 fails to compile this. But adding empty_('\0') to member
initializer list makes it happy.
This patch does the same for both OptionalStorage default constructors.
BUG=
Review-Url: https://codereview.chromium.org/2453733002
Cr-Commit-Position: refs/heads/master@{#427850}
CrOS-Libchrome-Original-Commit: a637f25fb515f8e860bc941cc1cb24b7862f3c5b
diff --git a/base/optional.h b/base/optional.h
index a32e068..17b3812 100644
--- a/base/optional.h
+++ b/base/optional.h
@@ -34,7 +34,9 @@
template <typename T, bool = base::is_trivially_destructible<T>::value>
struct OptionalStorage {
- constexpr OptionalStorage() {}
+ // Initializing |empty_| here instead of using default member initializing
+ // to avoid errors in g++ 4.8.
+ constexpr OptionalStorage() : empty_('\0') {}
constexpr explicit OptionalStorage(const T& value)
: is_null_(false), value_(value) {}
@@ -58,16 +60,18 @@
bool is_null_ = true;
union {
// |empty_| exists so that the union will always be initialized, even when
- // it doesn't contain a value. Not initializing it has been observed to
- // trigger comiler warnings.
- char empty_ = '\0';
+ // it doesn't contain a value. Union members must be initialized for the
+ // constructor to be 'constexpr'.
+ char empty_;
T value_;
};
};
template <typename T>
struct OptionalStorage<T, true> {
- constexpr OptionalStorage() {}
+ // Initializing |empty_| here instead of using default member initializing
+ // to avoid errors in g++ 4.8.
+ constexpr OptionalStorage() : empty_('\0') {}
constexpr explicit OptionalStorage(const T& value)
: is_null_(false), value_(value) {}
@@ -89,9 +93,9 @@
bool is_null_ = true;
union {
// |empty_| exists so that the union will always be initialized, even when
- // it doesn't contain a value. Not initializing it has been observed to
- // trigger comiler warnings.
- char empty_ = '\0';
+ // it doesn't contain a value. Union members must be initialized for the
+ // constructor to be 'constexpr'.
+ char empty_;
T value_;
};
};