Commit a695397e authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

In-place construction for Optional

Summary:
[Folly] In-place construction for `Optional`.

Using `in_place` as the first argument. Avoid move operations.

Reviewed By: Orvid, ot

Differential Revision: D5362563

fbshipit-source-id: 771db2556842d5dec35d1bf2ad6c23358a417d54
parent 8f8a5e47
......@@ -61,6 +61,7 @@
#include <utility>
#include <folly/Portability.h>
#include <folly/Utility.h>
namespace folly {
......@@ -126,6 +127,12 @@ class Optional {
construct(newValue);
}
template <typename... Args>
explicit Optional(in_place_t, Args&&... args)
noexcept(noexcept(::new (nullptr) Value(std::declval<Args&&>()...))) {
construct(std::forward<Args>(args)...);
}
void assign(const None&) {
clear();
}
......
......@@ -195,6 +195,21 @@ TEST(Optional, EmptyConstruct) {
EXPECT_FALSE(bool(test2));
}
TEST(Optional, InPlaceConstruct) {
using A = std::pair<int, double>;
Optional<A> opt(in_place, 5, 3.2);
EXPECT_TRUE(bool(opt));
EXPECT_EQ(5, opt->first);
}
TEST(Optional, InPlaceNestedConstruct) {
using A = std::pair<int, double>;
Optional<Optional<A>> opt(in_place, in_place, 5, 3.2);
EXPECT_TRUE(bool(opt));
EXPECT_TRUE(bool(*opt));
EXPECT_EQ(5, (*opt)->first);
}
TEST(Optional, Unique) {
Optional<unique_ptr<int>> opt;
......
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