From 3cdadfaa0fa95ec10f17e1847249852e6dbac85c Mon Sep 17 00:00:00 2001 From: Simon Brand Date: Fri, 7 Jun 2019 14:29:43 +0100 Subject: [PATCH] Allow binding of functions whose return types are convertible to R, fixes 9 --- function_ref.hpp | 4 ++-- tests/issues.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/function_ref.hpp b/function_ref.hpp index 36d2a55..a177e20 100644 --- a/function_ref.hpp +++ b/function_ref.hpp @@ -115,7 +115,7 @@ struct is_invocable_r_impl : std::false_type {}; template struct is_invocable_r_impl< - typename std::is_same, R>::type, R, F, Args...> + typename std::is_convertible, R>::type, R, F, Args...> : std::true_type {}; template @@ -152,7 +152,7 @@ template class function_ref { detail::is_invocable_r::value> * = nullptr> TL_FUNCTION_REF_11_CONSTEXPR function_ref(F &&f) noexcept : obj_(const_cast(reinterpret_cast(std::addressof(f)))) { - callback_ = [](void *obj, Args... args) { + callback_ = [](void *obj, Args... args) -> R { return detail::invoke( *reinterpret_cast::type>(obj), std::forward(args)...); diff --git a/tests/issues.cpp b/tests/issues.cpp index 098324f..6098839 100644 --- a/tests/issues.cpp +++ b/tests/issues.cpp @@ -5,4 +5,18 @@ TEST_CASE("Issue #2") { const auto lam = [](int ) {}; tl::function_ref ref = lam; (void)ref; +} + +struct Fruit {}; +struct Apple : Fruit {}; + +Apple* getApple() { return nullptr; } + +tl::function_ref bar() +{ + return getApple; +} + +TEST_CASE("Issue #9") { + REQUIRE(bar()() == nullptr); } \ No newline at end of file