Commit e8ab1931 authored by Matthias Vallentin's avatar Matthias Vallentin

Introduce new ctor overload for expected<T>

The new helper class no_error_t has a single constexpr instance
no_error, for which expected<T> now has a non-explicit ctor overload.
This comes in handy when a T has non-explicit ctor overload for none_t,
creating an ambiguity when attempting to construct expected<T>{none},
since both error and T are viable candidates.
parent cf7635fe
......@@ -33,6 +33,14 @@
namespace caf {
/// Helper class to construct an `expected<T>` that represents no error.
/// @relates expected
struct no_error_t {};
/// The only instance of ::no_error_t.
/// @relates expected
constexpr no_error_t no_error = no_error_t{};
/// Represents the result of a computation which can either complete
/// successfully with an instance of type `T` or fail with an `error`.
/// @tparam T The type of the result.
......@@ -76,6 +84,10 @@ public:
new (&error_) caf::error{std::move(e)};
}
expected(no_error_t) noexcept : engaged_(false) {
new (&error_) caf::error{};
}
expected(const expected& other) noexcept(nothrow_copy) {
construct(other);
}
......
......@@ -118,3 +118,13 @@ CAF_TEST(construction_with_none) {
CHECK(!x);
CHECK(!x.error());
}
CAF_TEST(construction_with_no_error) {
e_int x{no_error};
CHECK(!x);
CHECK(!x.error());
auto f = []() -> e_int {
return no_error;
};
CHECK_EQ(f(), x);
}
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