Commit 19a84207 authored by William McDonald's avatar William McDonald Committed by Facebook GitHub Bot

Add conversion from const folly::Optional to std::optional

Summary:
Previous version does not compile when converting from `const folly::Optional`
to `std::optional`. This happens in a typical `for (const auto& x : vec)` where
`x` is or contains `folly::Optional`.

Add a new `const` version in case people case about `Value&` ctor as opposed to
`const Value&`.

Reviewed By: iahs

Differential Revision: D26137442

fbshipit-source-id: 649be596b67a01c6d1f4be341aad65f1099b74a1
parent 339e7e6d
...@@ -188,7 +188,7 @@ class Optional { ...@@ -188,7 +188,7 @@ class Optional {
reset(); reset();
return ret; return ret;
} }
explicit operator std::optional<Value>() & noexcept( explicit operator std::optional<Value>() const& noexcept(
std::is_nothrow_copy_constructible<Value>::value) { std::is_nothrow_copy_constructible<Value>::value) {
return storage_.hasValue ? std::optional<Value>(storage_.value) return storage_.hasValue ? std::optional<Value>(storage_.value)
: std::nullopt; : std::nullopt;
......
...@@ -826,6 +826,10 @@ TEST(Optional, StdOptionalConversions) { ...@@ -826,6 +826,10 @@ TEST(Optional, StdOptionalConversions) {
EXPECT_EQ(*f, 42); EXPECT_EQ(*f, 42);
EXPECT_TRUE(s); EXPECT_TRUE(s);
const folly::Optional<int> fc = 12;
s = static_cast<std::optional<int>>(fc);
EXPECT_EQ(*s, 12);
folly::Optional<std::unique_ptr<int>> fp = std::make_unique<int>(42); folly::Optional<std::unique_ptr<int>> fp = std::make_unique<int>(42);
std::optional<std::unique_ptr<int>> sp(std::move(fp)); std::optional<std::unique_ptr<int>> sp(std::move(fp));
EXPECT_EQ(**sp, 42); EXPECT_EQ(**sp, 42);
......
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