From f1907836facb2955eba2e1acea309d9e5e725fdc Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Tue, 19 Sep 2023 08:17:31 +0200 Subject: [PATCH] Expected: Fix operator==(void, void) Change-Id: I883f05155f0710fc3a9f92a2e33d4f8722159016 Reviewed-by: David Schulz --- .../tl_expected/include/tl/expected.hpp | 15 +++++++++++++++ tests/auto/utils/expected/tst_expected.cpp | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/libs/3rdparty/tl_expected/include/tl/expected.hpp b/src/libs/3rdparty/tl_expected/include/tl/expected.hpp index d8ed472bdd1..56101d36331 100644 --- a/src/libs/3rdparty/tl_expected/include/tl/expected.hpp +++ b/src/libs/3rdparty/tl_expected/include/tl/expected.hpp @@ -2278,6 +2278,14 @@ detail::decay_t or_else_impl(Exp &&exp, F &&f) { #endif } // namespace detail +template +constexpr bool operator==(const expected &lhs, + const expected &rhs) { + return (lhs.has_value() != rhs.has_value()) + ? false + : (!lhs.has_value() ? lhs.error() == rhs.error() : true); +} + template constexpr bool operator==(const expected &lhs, const expected &rhs) { @@ -2292,6 +2300,13 @@ constexpr bool operator!=(const expected &lhs, ? true : (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs); } +template +constexpr bool operator!=(const expected &lhs, const expected &rhs) +{ + return (lhs.has_value() != rhs.has_value()) + ? true + : (!lhs.has_value() ? lhs.error() != rhs.error() : false); +} template constexpr bool operator==(const expected &x, const U &v) { diff --git a/tests/auto/utils/expected/tst_expected.cpp b/tests/auto/utils/expected/tst_expected.cpp index 43e58fb4f90..25240b861a7 100644 --- a/tests/auto/utils/expected/tst_expected.cpp +++ b/tests/auto/utils/expected/tst_expected.cpp @@ -32,6 +32,25 @@ private slots: QVERIFY(!result); } + + void tryCompareVoid() + { + tl::expected e1; + QVERIFY(e1 == e1); + + tl::expected e2 = make_unexpected("error"); + QVERIFY(e1 != e2); + + e1 = make_unexpected(QString("error")); + QVERIFY(e1 == e2); + + e2 = {}; + QVERIFY(e1 != e2); + + e1 = {}; + QVERIFY(e1 == e2); + QVERIFY(!(e1 != e2)); + } }; QTEST_GUILESS_MAIN(tst_expected)