Commit 16cc8501 authored by Dominik Charousset's avatar Dominik Charousset

Use addressof to guard against awful types

parent 502a5c95
...@@ -68,23 +68,23 @@ public: ...@@ -68,23 +68,23 @@ public:
expected(U x, expected(U x,
typename std::enable_if<std::is_convertible<U, T>::value>::type* = nullptr) typename std::enable_if<std::is_convertible<U, T>::value>::type* = nullptr)
: engaged_(true) { : engaged_(true) {
new (&value_) T(std::move(x)); new (std::addressof(value_)) T(std::move(x));
} }
expected(T&& x) noexcept(nothrow_move) : engaged_(true) { expected(T&& x) noexcept(nothrow_move) : engaged_(true) {
new (&value_) T(std::move(x)); new (std::addressof(value_)) T(std::move(x));
} }
expected(const T& x) noexcept(nothrow_copy) : engaged_(true) { expected(const T& x) noexcept(nothrow_copy) : engaged_(true) {
new (&value_) T(x); new (std::addressof(value_)) T(x);
} }
expected(caf::error e) noexcept : engaged_(false) { expected(caf::error e) noexcept : engaged_(false) {
new (&error_) caf::error{std::move(e)}; new (std::addressof(error_)) caf::error{std::move(e)};
} }
expected(no_error_t) noexcept : engaged_(false) { expected(no_error_t) noexcept : engaged_(false) {
new (&error_) caf::error{}; new (std::addressof(error_)) caf::error{};
} }
expected(const expected& other) noexcept(nothrow_copy) { expected(const expected& other) noexcept(nothrow_copy) {
...@@ -93,7 +93,7 @@ public: ...@@ -93,7 +93,7 @@ public:
template <class Code, class E = enable_if_has_make_error_t<Code>> template <class Code, class E = enable_if_has_make_error_t<Code>>
expected(Code code) : engaged_(false) { expected(Code code) : engaged_(false) {
new (&error_) caf::error(make_error(code)); new (std::addressof(error_)) caf::error(make_error(code));
} }
expected(expected&& other) noexcept(nothrow_move) { expected(expected&& other) noexcept(nothrow_move) {
...@@ -134,7 +134,7 @@ public: ...@@ -134,7 +134,7 @@ public:
} else { } else {
destroy(); destroy();
engaged_ = true; engaged_ = true;
new (&value_) T(x); new (std::addressof(value_)) T(x);
} }
return *this; return *this;
} }
...@@ -146,7 +146,7 @@ public: ...@@ -146,7 +146,7 @@ public:
} else { } else {
destroy(); destroy();
engaged_ = true; engaged_ = true;
new (&value_) T(std::move(x)); new (std::addressof(value_)) T(std::move(x));
} }
return *this; return *this;
} }
...@@ -163,7 +163,7 @@ public: ...@@ -163,7 +163,7 @@ public:
else { else {
destroy(); destroy();
engaged_ = false; engaged_ = false;
new (&error_) caf::error(std::move(e)); new (std::addressof(error_)) caf::error(std::move(e));
} }
return *this; return *this;
} }
...@@ -248,17 +248,17 @@ public: ...@@ -248,17 +248,17 @@ public:
private: private:
void construct(expected&& other) noexcept(nothrow_move) { void construct(expected&& other) noexcept(nothrow_move) {
if (other.engaged_) if (other.engaged_)
new (&value_) T(std::move(other.value_)); new (std::addressof(value_)) T(std::move(other.value_));
else else
new (&error_) caf::error(std::move(other.error_)); new (std::addressof(error_)) caf::error(std::move(other.error_));
engaged_ = other.engaged_; engaged_ = other.engaged_;
} }
void construct(const expected& other) noexcept(nothrow_copy) { void construct(const expected& other) noexcept(nothrow_copy) {
if (other.engaged_) if (other.engaged_)
new (&value_) T(other.value_); new (std::addressof(value_)) T(other.value_);
else else
new (&error_) caf::error(other.error_); new (std::addressof(error_)) caf::error(other.error_);
engaged_ = other.engaged_; engaged_ = other.engaged_;
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#pragma once #pragma once
#include <memory>
#include <new> #include <new>
#include <utility> #include <utility>
...@@ -157,7 +158,7 @@ class optional { ...@@ -157,7 +158,7 @@ class optional {
void cr(V&& x) { void cr(V&& x) {
CAF_ASSERT(!m_valid); CAF_ASSERT(!m_valid);
m_valid = true; m_valid = true;
new (&m_value) T(std::forward<V>(x)); new (std::addressof(m_value)) T(std::forward<V>(x));
} }
bool m_valid; bool m_valid;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#pragma once #pragma once
#include <functional> #include <functional>
#include <memory>
#include <type_traits> #include <type_traits>
#include "caf/config.hpp" #include "caf/config.hpp"
...@@ -280,7 +281,7 @@ private: ...@@ -280,7 +281,7 @@ private:
destroy_data(); destroy_data();
type_ = type_id; type_ = type_id;
auto& ref = data_.get(token); auto& ref = data_.get(token);
new (&ref) type (std::forward<U>(arg)); new (std::addressof(ref)) type (std::forward<U>(arg));
} else { } else {
data_.get(token) = std::forward<U>(arg); data_.get(token) = std::forward<U>(arg);
} }
......
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