Skip to content

Commit 3cdadfa

Browse files
committed
Allow binding of functions whose return types are convertible to R, fixes 9
1 parent b2b125e commit 3cdadfa

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

function_ref.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct is_invocable_r_impl : std::false_type {};
115115

116116
template <class R, class F, class... Args>
117117
struct is_invocable_r_impl<
118-
typename std::is_same<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
118+
typename std::is_convertible<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
119119
: std::true_type {};
120120

121121
template <class R, class F, class... Args>
@@ -152,7 +152,7 @@ template <class R, class... Args> class function_ref<R(Args...)> {
152152
detail::is_invocable_r<R, F &&, Args...>::value> * = nullptr>
153153
TL_FUNCTION_REF_11_CONSTEXPR function_ref(F &&f) noexcept
154154
: obj_(const_cast<void*>(reinterpret_cast<const void *>(std::addressof(f)))) {
155-
callback_ = [](void *obj, Args... args) {
155+
callback_ = [](void *obj, Args... args) -> R {
156156
return detail::invoke(
157157
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
158158
std::forward<Args>(args)...);

tests/issues.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@ TEST_CASE("Issue #2") {
55
const auto lam = [](int ) {};
66
tl::function_ref<void(int)> ref = lam;
77
(void)ref;
8+
}
9+
10+
struct Fruit {};
11+
struct Apple : Fruit {};
12+
13+
Apple* getApple() { return nullptr; }
14+
15+
tl::function_ref<Fruit* ()> bar()
16+
{
17+
return getApple;
18+
}
19+
20+
TEST_CASE("Issue #9") {
21+
REQUIRE(bar()() == nullptr);
822
}

0 commit comments

Comments
 (0)