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

Disallow constructing folly::Optional with nullptr

Summary:
Disallow initializing folly::Optional with nullptr as if it were
folly::none. The previous behavior was a footgun when transitioning to
C++17's std::optional. For example, given:

```
folly::Optional<bool> o1 = nullptr;
std::optional<bool> o2 = nullptr;
```

o1 would be none, but o2 would have the value `{false}`.

This diff makes the former illegal, preventing behavior changes
when transitioning to std::optional.

Reviewed By: yfeldblum

Differential Revision: D12843022

fbshipit-source-id: 165d19a963672c04d9ec687cb687ca89f1837e21
parent c339c77a
...@@ -134,14 +134,23 @@ class Optional { ...@@ -134,14 +134,23 @@ class Optional {
} }
/** /**
* DEPRECATED: use folly::none * 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> template <typename Null = std::nullptr_t>
FOLLY_CPP14_CONSTEXPR /* implicit */ /* implicit */
Optional(typename std::enable_if< Optional(typename std::enable_if<
!std::is_pointer<Value>::value && std::is_convertible<Null, Value>::value,
std::is_same<Null, std::nullptr_t>::value, Null>::type) noexcept = delete;
Null>::type) noexcept {}
template <typename... Args> template <typename... Args>
FOLLY_CPP14_CONSTEXPR explicit Optional(in_place_t, Args&&... args) noexcept( FOLLY_CPP14_CONSTEXPR explicit Optional(in_place_t, Args&&... args) noexcept(
......
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