Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 12 additions & 21 deletions include/zeus/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ inline constexpr bool is_copy_assignable_or_void_v = is_void_or_v<T, std::is_cop
template<class T>
inline constexpr bool is_move_assignable_or_void_v = is_void_or_v<T, std::is_move_assignable<T>>;

template<class From, class To>
inline constexpr bool is_nothrow_convertible_v = noexcept(static_cast<To>(std::declval<From>()));

} // namespace expected_detail

template<class E>
Expand Down Expand Up @@ -1681,9 +1684,7 @@ class expected

template<class U>
constexpr T value_or(U &&v) const & //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<T> && std::is_nothrow_convertible_v<U, T>)
#endif
noexcept(std::is_nothrow_copy_constructible_v<T> && expected_detail::is_nothrow_convertible_v<U, T>)
{
static_assert(std::is_copy_constructible_v<T>, "T must be copy-constructible");
static_assert(std::is_convertible_v<U, T>, "is_convertible_v<U, T> must be true");
Expand All @@ -1698,11 +1699,9 @@ class expected
}
template<class U>
constexpr T value_or(U &&v) && //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<T> && std::is_nothrow_convertible_v<U, T>)
#endif
noexcept(std::is_nothrow_move_constructible_v<T> && expected_detail::is_nothrow_convertible_v<U, T>)
{
static_assert(std::is_copy_constructible_v<T>, "T must be copy-constructible");
static_assert(std::is_move_constructible_v<T>, "T must be move-constructible");
static_assert(std::is_convertible_v<U, T>, "is_convertible_v<U, T> must be true");
if (this->m_has_val)
{
Expand All @@ -1716,9 +1715,7 @@ class expected

template<class G = E>
constexpr E error_or(G &&v) const & //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<E> && std::is_nothrow_convertible_v<G, E>)
#endif
noexcept(std::is_nothrow_copy_constructible_v<E> && expected_detail::is_nothrow_convertible_v<G, E>)
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy-constructible");
static_assert(std::is_convertible_v<G, E>, "is_convertible_v<G, E> must be true");
Expand All @@ -1733,11 +1730,9 @@ class expected
}
template<class G = E>
constexpr E error_or(G &&v) && //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<E> && std::is_nothrow_convertible_v<G, E>)
#endif
noexcept(std::is_nothrow_move_constructible_v<E> && expected_detail::is_nothrow_convertible_v<G, E>)
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy-constructible");
static_assert(std::is_move_constructible_v<E>, "E must be move-constructible");
static_assert(std::is_convertible_v<G, E>, "is_convertible_v<G, E> must be true");
if (this->m_has_val)
{
Expand Down Expand Up @@ -2407,9 +2402,7 @@ class expected<void, E>

template<class G = E>
constexpr E error_or(G &&v) const & //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<E> && std::is_nothrow_convertible_v<G, E>)
#endif
noexcept(std::is_nothrow_copy_constructible_v<E> && expected_detail::is_nothrow_convertible_v<G, E>)
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy-constructible");
static_assert(std::is_convertible_v<G, E>, "is_convertible_v<G, E> must be true");
Expand All @@ -2424,11 +2417,9 @@ class expected<void, E>
}
template<class G = E>
constexpr E error_or(G &&v) && //
#if __cplusplus >= 202'002L // doesn't compile with C++17 with MSVC, may be a compiler bug
noexcept(std::is_nothrow_copy_constructible_v<E> && std::is_nothrow_convertible_v<G, E>)
#endif
noexcept(std::is_nothrow_move_constructible_v<E> && expected_detail::is_nothrow_convertible_v<G, E>)
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy-constructible");
static_assert(std::is_move_constructible_v<E>, "E must be move-constructible");
static_assert(std::is_convertible_v<G, E>, "is_convertible_v<G, E> must be true");
if (this->m_has_val)
{
Expand Down
1 change: 1 addition & 0 deletions tests/test_expected/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(test_expected)
set(SOURCES
base_tests.cpp
monadic_tests.cpp
noexcept_tests.cpp
test_expected_main.cpp
)

Expand Down
90 changes: 90 additions & 0 deletions tests/test_expected/noexcept_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <catch2/catch_all.hpp>

#include <zeus/expected.hpp>

struct FromType
{
};

enum class NoThrowConvertible
{
Yes,
No,
};

template<NoThrowConvertible N>
struct ToType;

template<>
struct ToType<NoThrowConvertible::Yes>
{
ToType() = default;
ToType(ToType const&) = default;
ToType(ToType&&) = default;

ToType(FromType const&) noexcept {}
};

template<>
struct ToType<NoThrowConvertible::No>
{
ToType() = default;
ToType(ToType const&) = default;
ToType(ToType&&) = default;

ToType(FromType const&) noexcept(false) {}
};

TEST_CASE("value_or() noexcept", "[noexcept, value_or]")
{
{ // noexcept(true)
using T = ToType<NoThrowConvertible::Yes>;
using E = int;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK(noexcept(e.value_or(FromType {})));
}
{ // noexcept(false)
using T = ToType<NoThrowConvertible::No>;
using E = int;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK_FALSE(noexcept(e.value_or(FromType {})));
}
}

TEST_CASE("error_or() noexcept", "[noexcept, error_or]")
{
{ // noexcept(true)
using T = int;
using E = ToType<NoThrowConvertible::Yes>;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK(noexcept(e.error_or(FromType {})));
}
{ // noexcept(false)
using T = int;
using E = ToType<NoThrowConvertible::No>;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK_FALSE(noexcept(e.error_or(FromType {})));
}
}

TEST_CASE("void-T error_or() noexcept", "[noexcept, error_or, void-T]")
{
{ // noexcept(true)
using T = void;
using E = ToType<NoThrowConvertible::Yes>;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK(noexcept(e.error_or(FromType {})));
}
{ // noexcept(false)
using T = void;
using E = ToType<NoThrowConvertible::No>;
using Expected = zeus::expected<T, E>;
Expected e;
CHECK_FALSE(noexcept(e.error_or(FromType {})));
}
}