Skip to content

A minimal std::expected<T, E> #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup: Add operator-> and additional observers
  • Loading branch information
deanberris committed Feb 1, 2019
commit 411094e6f1c4e0f65be8b212bd411dab1f925b6a
73 changes: 72 additions & 1 deletion netlib/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,72 @@ class expected {
new (&union_storage_) unexpected<E>(std::move(e.value()));
}

constexpr explicit operator bool() const noexcept { return has_value_; }
// Destructor.
~expected() {
if constexpr (!std::is_trivially_destructible_v<T>) {
if (has_value_)
reinterpret_cast<T*>(&union_storage_)->~T();
else
reinterpret_cast<unexpected_type*>(&union_storage_)->~unexpected_type();
}
}

// Assignment.
expected& operator=(const expected& other) {
if (has_value_ and other.has_value_) {
**this = *other;
return *this;
}

if (!has_value_ and !other.has_value_) {
(*reinterpret_cast<unexpected_type*>(&union_storage_)) =
unexpected(other.error());
return *this;
}

if (has_value_ and !other.has_value_) {
reinterpret_cast<T*>(&union_storage_)->~T();
new (&union_storage_) unexpected_type(unexpected(other.error()));
has_value_ = false;
return *this;
}

if (!has_value_ and other.has_value_) {
if constexpr (std::is_nothrow_copy_constructible_v<T>) {
reinterpret_cast<unexpected_type*>(&union_storage_)->~unexpected_type();
new (&union_storage_) T(other.value());
has_value_ = true;
} else if constexpr (std::is_nothrow_move_constructible_v<T>) {
T tmp = *other;
reinterpret_cast<unexpected_type*>(&union_storage_)->~unexpected_type();
new (&union_storage_) T(tmp);
has_value_ = true;
} else if constexpr (std::is_nothrow_move_constructible_v<E>) {
unexpected_type tmp = unexpected(std::move(error()));
reinterpret_cast<unexpected_type*>(&union_storage_)->~unexpected_type();
try {
new (&union_storage_) T(*other);
has_value_ = true;
} catch (...) {
new (&union_storage_) unexpected_type(std::move(tmp));
}
}
}

return *this;
}

// Observer functions.
constexpr const T* operator->() const {
if (!has_value_) throw bad_expected_access<E>(error());
return reinterpret_cast<const T*>(&union_storage_);
}

constexpr T* operator->() {
if (!has_value_) throw bad_expected_access<E>(error());
return reinterpret_cast<T*>(&union_storage_);
}

constexpr const T& operator*() const& {
if (!has_value_) throw bad_expected_access<E>(error());
return *reinterpret_cast<const T*>(&union_storage_);
Expand All @@ -252,6 +315,14 @@ class expected {
return std::move(*reinterpret_cast<T*>(&union_storage_));
}

constexpr explicit operator bool() const noexcept { return has_value_; }
constexpr bool has_value() const noexcept { return has_value_; }

constexpr const T& value() const& { return **this; }
constexpr T& value() & { return **this; }
constexpr const T&& value() const&& { return std::move(**this); }
constexpr T&& value() && { return std::move(**this); }

constexpr const E& error() const& {
assert(!has_value_ &&
"expected<T, E> must not have a value when taking an error!");
Expand Down
8 changes: 7 additions & 1 deletion netlib/expected_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ TEST(ExpectedTest, Unexpected) {
ASSERT_THROW(*e2, bad_expected_access<error_code>);

auto e3 = g(true, 1, 2);
ASSERT_FALSE(e2);
ASSERT_FALSE(e3);
EXPECT_THAT(e3.error(), Eq(errors::undefined));
ASSERT_THROW(*e3, bad_expected_access<error_code>);

e3 = g(false, 2, 3);
ASSERT_TRUE(e3);
ASSERT_THAT(e3->first, Eq(2));
ASSERT_THAT(e3->second, Eq(3));
}

} // namespace
Expand Down