Commit 6b97363a authored by Chad Austin's avatar Chad Austin Committed by Facebook Github Bot

allow conversion from nullptr to folly::Optional<bool>

Summary:
Disabling the nullptr conversion to non-pointer folly::Optional<T> has
baked long enough. Re-enabled it with the same behavior that
std::optional has.

Reviewed By: yfeldblum

Differential Revision: D15057633

fbshipit-source-id: 6ba47a57a78ec4834ed4ecc8987a7c8870102ae4
parent e40b0e44
...@@ -133,25 +133,6 @@ class Optional { ...@@ -133,25 +133,6 @@ class Optional {
construct(newValue); construct(newValue);
} }
/**
* Explicitly disallow converting nullptr to non-pointer
* types. Optional used to support initialization from nullptr as if
* it were folly::none. Without this constructor,
* folly::Optional<bool> could be constructed from nullptr,
* producing {false} instead of {}. This would be a change in
* behavior from the old code, so explicitly disallow it. Note that
* std::optional<bool> can be constructed from nullptr, also
* producing {false}.
*
* This constructor is temporary and should be removed when all call
* sites are fixed.
*/
template <typename Null = std::nullptr_t>
/* implicit */
Optional(typename std::enable_if<
std::is_convertible<Null, Value>::value,
Null>::type) noexcept = delete;
template <typename... Args> template <typename... Args>
constexpr explicit Optional(in_place_t, Args&&... args) noexcept( constexpr explicit Optional(in_place_t, Args&&... args) noexcept(
std::is_nothrow_constructible<Value, Args...>::value) std::is_nothrow_constructible<Value, Args...>::value)
......
...@@ -667,6 +667,18 @@ TEST(Optional, InitializerListConstruct) { ...@@ -667,6 +667,18 @@ TEST(Optional, InitializerListConstruct) {
std::ignore = optional; std::ignore = optional;
} }
TEST(Optional, ConversionFromNullptr) {
// Match the behavior of std::optional
static constexpr void* null = nullptr;
folly::Optional<bool> o1{null};
folly::Optional<bool> o2(null);
folly::Optional<bool> o3 = null;
EXPECT_EQ(false, o1.value());
EXPECT_EQ(false, o2.value());
EXPECT_EQ(false, o3.value());
}
TEST(Optional, TestDisambiguationMakeOptionalVariants) { TEST(Optional, TestDisambiguationMakeOptionalVariants) {
{ {
auto optional = make_optional<int>(1); auto optional = make_optional<int>(1);
......
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