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
19 changes: 13 additions & 6 deletions include/zeus/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ static_assert(false, "This expected variant requires C++17");
#define ZEUS_EXPECTED_CONSTEXPR_DTOR
#endif

// Detect exception support
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
#define ZEUS_EXPECTED_THROW(e) (throw (e))
#else
#define ZEUS_EXPECTED_THROW(e) ((void)(e), std::terminate())
#endif

#define ZEUS_EXPECTED_ABI_TAG expected_abi

#define ZEUS_EXPECTED_NS_VERSION_CONCAT_EX(major, minor, patch) _v##major##_##minor##_##patch
Expand Down Expand Up @@ -1657,30 +1664,30 @@ class expected
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3843");
if (!has_value())
throw bad_expected_access(error());
ZEUS_EXPECTED_THROW(bad_expected_access(error()));
return val();
}
constexpr T &value() &
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3843");
if (!has_value())
throw bad_expected_access(std::as_const(error()));
ZEUS_EXPECTED_THROW(bad_expected_access(std::as_const(error())));
return val();
}
constexpr const T &&value() const &&
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3843");
static_assert(std::is_constructible_v<E, decltype(std::move(error()))>, "E must be constructible from const E&&, by LWG-3843");
if (!has_value())
throw bad_expected_access(std::move(error()));
ZEUS_EXPECTED_THROW(bad_expected_access(std::move(error())));
return std::move(val());
}
constexpr T &&value() &&
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3843");
static_assert(std::is_constructible_v<E, decltype(std::move(error()))>, "E must be constructible from E&&, by LWG-3843");
if (!has_value())
throw bad_expected_access(std::move(error()));
ZEUS_EXPECTED_THROW(bad_expected_access(std::move(error())));
return std::move(val());
}

Expand Down Expand Up @@ -2428,15 +2435,15 @@ class expected<void, E>
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3940");
if (!has_value())
throw bad_expected_access(err());
ZEUS_EXPECTED_THROW(bad_expected_access(err()));
}

constexpr void value() &&
{
static_assert(std::is_copy_constructible_v<E>, "E must be copy constructible, by LWG-3940");
static_assert(std::is_move_constructible_v<E>, "E must be move constructible, by LWG-3940");
if (!has_value())
throw bad_expected_access(std::move(err()));
ZEUS_EXPECTED_THROW(bad_expected_access(std::move(err())));
}

constexpr const E &error() const & noexcept //
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(test_expected)
add_subdirectory(test_no_exceptions)
add_subdirectory(third_party)
13 changes: 13 additions & 0 deletions tests/test_no_exceptions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project(test_no_exceptions)

add_executable(${PROJECT_NAME} test_no_exceptions.cpp)
target_link_libraries(${PROJECT_NAME} zeus::expected)

if (MSVC)
target_compile_options(${PROJECT_NAME} PRIVATE /EHs-c-)
target_compile_definitions(${PROJECT_NAME} PRIVATE _HAS_EXCEPTIONS=0)
else ()
target_compile_options(${PROJECT_NAME} PRIVATE -fno-exceptions)
endif ()

add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
50 changes: 50 additions & 0 deletions tests/test_no_exceptions/test_no_exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <zeus/expected.hpp>

#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
static_assert(false, "This test must be compiled with exceptions disabled");
#endif

zeus::expected<int, int> try_parse(bool succeed)
{
if (succeed)
return 42;
return zeus::unexpected(1);
}

int main()
{
auto e = try_parse(true);
if (!e.has_value())
return 1;
if (*e != 42)
return 2;

auto e2 = try_parse(false);
if (e2.has_value())
return 3;
if (e2.error() != 1)
return 4;

if (e2.value_or(99) != 99)
return 5;

auto e3 = e.and_then([](int v) -> zeus::expected<int, int> { return v + 1; });
if (*e3 != 43)
return 6;

auto e4 = e2.or_else([](int) -> zeus::expected<int, int> { return 0; });
if (*e4 != 0)
return 7;

zeus::expected<void, int> ev;
if (!ev.has_value())
return 8;

zeus::expected<void, int> ev2{zeus::unexpect, 10};
if (ev2.has_value())
return 9;
if (ev2.error() != 10)
return 10;

return 0;
}
Loading