From fd2c94bce1ffc01fd0632d3e4145adc04a6476c8 Mon Sep 17 00:00:00 2001 From: Somesh Daga Date: Tue, 9 Jan 2024 16:50:55 -0500 Subject: [PATCH] Fix constructor overload resolution for derived classes --- include/tl/expected.hpp | 4 ++-- tests/derived.cpp | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/derived.cpp diff --git a/include/tl/expected.hpp b/include/tl/expected.hpp index afee404..a498751 100644 --- a/include/tl/expected.hpp +++ b/include/tl/expected.hpp @@ -398,8 +398,8 @@ template using expected_enable_forward_value = detail::enable_if_t< std::is_constructible::value && !std::is_same, in_place_t>::value && - !std::is_same, detail::decay_t>::value && - !std::is_same, detail::decay_t>::value>; + !std::is_base_of, detail::decay_t>::value && + !std::is_base_of, detail::decay_t>::value>; template using expected_enable_from_other = detail::enable_if_t< diff --git a/tests/derived.cpp b/tests/derived.cpp new file mode 100644 index 0000000..2d1a859 --- /dev/null +++ b/tests/derived.cpp @@ -0,0 +1,53 @@ +#include +#include + +namespace tl { + template + class derived : public tl::expected + { + public: + derived() : tl::expected() {} + template + derived(unexpected const &e) : tl::expected(e) {} + derived(const derived& d) : tl::expected(d) {} + derived(derived&& d) : tl::expected(std::move(d)) {} + // TODO: Add overloads for other constructors + }; +} + +TEST_CASE("Derived constructors", "[constructors.derived]") { + { + tl::derived e; + REQUIRE(e); + REQUIRE(e == 0); + } + + // Construct from unexpected + { + tl::derived e = tl::make_unexpected(0); + REQUIRE(!e); + REQUIRE(e.error() == 0); + } + + // Copy construct + { + tl::derived e; + REQUIRE(e); + REQUIRE(!e.value()); + + tl::derived e_copy(e); + REQUIRE(e_copy); + REQUIRE(e_copy.value() == e.value()); + } + + // Move construct + { + tl::derived e; + REQUIRE(e); + REQUIRE(e == false); + + tl::derived e_move(std::move(e)); + REQUIRE(e_move); + REQUIRE(!e_move.value()); + } +} \ No newline at end of file