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

Use addressof to guard against awful types

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