Commit cc15e578 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

rely on C++17 syntactic copy elision in DelayedInit

Summary: Rely on C++17 syntactic copy elision in `DelayedInit` to simplify and write `try_emplace` in terms of `try_emplace_with`.

Reviewed By: praihan

Differential Revision: D26260351

fbshipit-source-id: 590fdfed55ee325ed8a8ae4fa68c1db0bebf1545
parent b93c8daa
...@@ -81,7 +81,8 @@ struct DelayedInit { ...@@ -81,7 +81,8 @@ struct DelayedInit {
*/ */
template <typename Func> template <typename Func>
T& try_emplace_with(Func func) { T& try_emplace_with(Func func) {
call_once(storage_.init, [&] { ::new (slot()) T(func()); }); auto addr = static_cast<void*>(std::addressof(storage_.value));
call_once(storage_.init, [&] { ::new (addr) T(func()); });
return storage_.value; return storage_.value;
} }
...@@ -91,8 +92,7 @@ struct DelayedInit { ...@@ -91,8 +92,7 @@ struct DelayedInit {
*/ */
template <typename... A> template <typename... A>
T& try_emplace(A&&... a) { T& try_emplace(A&&... a) {
call_once(storage_.init, [&] { store(static_cast<A&&>(a)...); }); return try_emplace_with([&] { return T(static_cast<A&&>(a)...); });
return storage_.value;
} }
template < template <
typename U, typename U,
...@@ -100,8 +100,7 @@ struct DelayedInit { ...@@ -100,8 +100,7 @@ struct DelayedInit {
typename = std::enable_if_t< typename = std::enable_if_t<
std::is_constructible<T, std::initializer_list<U>, A...>::value>> std::is_constructible<T, std::initializer_list<U>, A...>::value>>
T& try_emplace(std::initializer_list<U> ilist, A&&... a) { T& try_emplace(std::initializer_list<U> ilist, A&&... a) {
call_once(storage_.init, [&] { store(ilist, static_cast<A&&>(a)...); }); return try_emplace_with([&] { return T(ilist, static_cast<A&&>(a)...); });
return storage_.value;
} }
bool has_value() const { return test_once(storage_.init); } bool has_value() const { return test_once(storage_.init); }
...@@ -174,13 +173,6 @@ struct DelayedInit { ...@@ -174,13 +173,6 @@ struct DelayedInit {
StorageTriviallyDestructible, StorageTriviallyDestructible,
StorageNonTriviallyDestructible>; StorageNonTriviallyDestructible>;
void* slot() { return static_cast<void*>(std::addressof(storage_.value)); }
template <typename... A>
void store(A&&... a) {
::new (slot()) T(static_cast<A&&>(a)...);
}
Storage storage_; Storage storage_;
}; };
......
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