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

Use ::new and std::addressof in Try

Summary: [Folly] Use `::new` and `std::addressof` in `Try` to evade custom overloads of `operator new` and of `operator&` in certain types.

Reviewed By: lewissbaker

Differential Revision: D24698131

fbshipit-source-id: 9b4a3a5e9962018450104ee0d6df2cf58a2881b7
parent 120fb0fd
......@@ -29,7 +29,7 @@ template <class T>
Try<T>::Try(Try<T>&& t) noexcept(std::is_nothrow_move_constructible<T>::value)
: contains_(t.contains_) {
if (contains_ == Contains::VALUE) {
new (&value_) T(std::move(t.value_));
::new (static_cast<void*>(std::addressof(value_))) T(std::move(t.value_));
} else if (contains_ == Contains::EXCEPTION) {
new (&e_) exception_wrapper(std::move(t.e_));
}
......@@ -43,7 +43,7 @@ Try<T>::Try(typename std::enable_if<
: contains_(Contains::NOTHING) {
if (t.hasValue()) {
contains_ = Contains::VALUE;
new (&value_) T();
::new (static_cast<void*>(std::addressof(value_))) T();
} else if (t.hasException()) {
contains_ = Contains::EXCEPTION;
new (&e_) exception_wrapper(t.exception());
......@@ -66,7 +66,7 @@ Try<T>& Try<T>::operator=(Try<T>&& t) noexcept(
destroy();
if (t.contains_ == Contains::VALUE) {
new (&value_) T(std::move(t.value_));
::new (static_cast<void*>(std::addressof(value_))) T(std::move(t.value_));
} else if (t.contains_ == Contains::EXCEPTION) {
new (&e_) exception_wrapper(std::move(t.e_));
}
......@@ -84,7 +84,7 @@ Try<T>::Try(const Try<T>& t) noexcept(
"T must be copyable for Try<T> to be copyable");
contains_ = t.contains_;
if (contains_ == Contains::VALUE) {
new (&value_) T(t.value_);
::new (static_cast<void*>(std::addressof(value_))) T(t.value_);
} else if (contains_ == Contains::EXCEPTION) {
new (&e_) exception_wrapper(t.e_);
}
......@@ -104,7 +104,7 @@ Try<T>& Try<T>::operator=(const Try<T>& t) noexcept(
destroy();
if (t.contains_ == Contains::VALUE) {
new (&value_) T(t.value_);
::new (static_cast<void*>(std::addressof(value_))) T(t.value_);
} else if (t.contains_ == Contains::EXCEPTION) {
new (&e_) exception_wrapper(t.e_);
}
......@@ -128,7 +128,8 @@ template <typename... Args>
T& Try<T>::emplace(Args&&... args) noexcept(
std::is_nothrow_constructible<T, Args&&...>::value) {
this->destroy();
new (&value_) T(static_cast<Args&&>(args)...);
::new (static_cast<void*>(std::addressof(value_)))
T(static_cast<Args&&>(args)...);
contains_ = Contains::VALUE;
return value_;
}
......
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