Commit b759bef8 authored by Scott Wolchok's avatar Scott Wolchok Committed by Facebook Github Bot

Make Optional<int> usable as constexpr (if initialized to none)

Summary: StorageTriviallyDestructible needed a constexpr ctor.

Reviewed By: yfeldblum, ot, luciang

Differential Revision: D14944492

fbshipit-source-id: cd9d39d7cb0719e7830887f548eabcc0defa1aee
parent a2b8b96a
......@@ -399,7 +399,8 @@ class Optional {
};
bool hasValue;
StorageTriviallyDestructible() : hasValue{false} {}
constexpr StorageTriviallyDestructible()
: emptyState('\0'), hasValue{false} {}
void clear() {
hasValue = false;
}
......
......@@ -63,6 +63,18 @@ static_assert(sizeof(Optional<short>) == sizeof(boost::optional<short>), "");
static_assert(sizeof(Optional<int>) == sizeof(boost::optional<int>), "");
static_assert(sizeof(Optional<double>) == sizeof(boost::optional<double>), "");
TEST(Optional, ConstexprConstructible) {
// Use FOLLY_STORAGE_CONSTEXPR to work around MSVC not taking this.
static FOLLY_STORAGE_CONSTEXPR Optional<int> opt;
// NOTE: writing `opt = none` instead of `opt(none)` causes gcc to reject this
// code, claiming that the (non-constexpr) move ctor of `Optional` is being
// invoked.
static FOLLY_STORAGE_CONSTEXPR Optional<int> opt2(none);
EXPECT_FALSE(opt.has_value());
EXPECT_FALSE(opt2.has_value());
}
TEST(Optional, NoDefault) {
Optional<NoDefault> x;
EXPECT_FALSE(x);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment