mirror of
https://github.com/TartanLlama/expected.git
synced 2025-08-02 18:34:29 +02:00
Add variadic constructors for tl::unexpected
This commit is contained in:
@@ -138,6 +138,19 @@ public:
|
|||||||
|
|
||||||
constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {}
|
constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {}
|
||||||
|
|
||||||
|
|
||||||
|
template <class... Args, typename std::enable_if<std::is_constructible<
|
||||||
|
E, Args &&...>::value>::type * = nullptr>
|
||||||
|
constexpr explicit unexpected(Args &&...args)
|
||||||
|
: m_val(std::forward<Args>(args)...) {}
|
||||||
|
template <
|
||||||
|
class U, class... Args,
|
||||||
|
typename std::enable_if<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value>::type * = nullptr>
|
||||||
|
constexpr explicit unexpected(std::initializer_list<U> l,
|
||||||
|
Args &&...args)
|
||||||
|
: m_val(l, std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
constexpr const E &value() const & { return m_val; }
|
constexpr const E &value() const & { return m_val; }
|
||||||
TL_EXPECTED_11_CONSTEXPR E &value() & { return m_val; }
|
TL_EXPECTED_11_CONSTEXPR E &value() & { return m_val; }
|
||||||
TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); }
|
TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); }
|
||||||
|
@@ -164,6 +164,23 @@ TEST_CASE("Issue 122", "[issues.122]") {
|
|||||||
#ifdef __cpp_deduction_guides
|
#ifdef __cpp_deduction_guides
|
||||||
TEST_CASE("Issue 89", "[issues.89]") {
|
TEST_CASE("Issue 89", "[issues.89]") {
|
||||||
auto s = tl::unexpected("Some string");
|
auto s = tl::unexpected("Some string");
|
||||||
REQUIRE(s.value() == "Some string");
|
REQUIRE(s.value() == std::string("Some string"));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct S {
|
||||||
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
|
S(int i) : i(i) {}
|
||||||
|
S(int i, int j) : i(i), j(j) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("Issue 107", "[issues.107]") {
|
||||||
|
tl::expected<int, S> ex1(tl::unexpect, 2);
|
||||||
|
tl::expected<int, S> ex2(tl::unexpect, 2, 2);
|
||||||
|
|
||||||
|
REQUIRE(ex1.error().i == 2);
|
||||||
|
REQUIRE(ex1.error().j == 0);
|
||||||
|
REQUIRE(ex2.error().i == 2);
|
||||||
|
REQUIRE(ex2.error().j == 2);
|
||||||
|
}
|
Reference in New Issue
Block a user