Skip to content

Commit

Permalink
Allow binding of functions whose return types are convertible to R, f…
Browse files Browse the repository at this point in the history
…ixes 9
  • Loading branch information
TartanLlama committed Jun 7, 2019
1 parent b2b125e commit 3cdadfa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions function_ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct is_invocable_r_impl : std::false_type {};

template <class R, class F, class... Args>
struct is_invocable_r_impl<
typename std::is_same<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
typename std::is_convertible<invoke_result_t<F, Args...>, R>::type, R, F, Args...>
: std::true_type {};

template <class R, class F, class... Args>
Expand Down Expand Up @@ -152,7 +152,7 @@ template <class R, class... Args> class function_ref<R(Args...)> {
detail::is_invocable_r<R, F &&, Args...>::value> * = nullptr>
TL_FUNCTION_REF_11_CONSTEXPR function_ref(F &&f) noexcept
: obj_(const_cast<void*>(reinterpret_cast<const void *>(std::addressof(f)))) {
callback_ = [](void *obj, Args... args) {
callback_ = [](void *obj, Args... args) -> R {
return detail::invoke(
*reinterpret_cast<typename std::add_pointer<F>::type>(obj),
std::forward<Args>(args)...);
Expand Down
14 changes: 14 additions & 0 deletions tests/issues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@ TEST_CASE("Issue #2") {
const auto lam = [](int ) {};
tl::function_ref<void(int)> ref = lam;
(void)ref;
}

struct Fruit {};
struct Apple : Fruit {};

Apple* getApple() { return nullptr; }

tl::function_ref<Fruit* ()> bar()
{
return getApple;
}

TEST_CASE("Issue #9") {
REQUIRE(bar()() == nullptr);
}

0 comments on commit 3cdadfa

Please sign in to comment.