From 065d97b585a69c20a30e7ea8ac5413cb6125118a Mon Sep 17 00:00:00 2001 From: tusooa Date: Mon, 5 Feb 2024 18:03:51 -0500 Subject: [PATCH] Make zug::compat::invoke work with pointer-to-member function --- test/transducer/map.cpp | 19 +++++++++++++++++++ zug/compat/invoke.hpp | 9 +++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/test/transducer/map.cpp b/test/transducer/map.cpp index bec37f5..467e3fc 100644 --- a/test/transducer/map.cpp +++ b/test/transducer/map.cpp @@ -36,6 +36,11 @@ namespace { int free_times2(int x) { return x * 2; } +struct Foo { + int a; + const int &getA() const { return a; } +}; + } // anonymous namespace TEST_CASE("map, mapping invoke") @@ -43,3 +48,17 @@ TEST_CASE("map, mapping invoke") auto v = std::vector{1, 2, 3, 6}; CHECK(transduce(map(&free_times2), std::plus{}, 1, v) == 25); } + +TEST_CASE("map, pointer to member function") +{ + auto v = std::vector{{1}}; + auto r = into_vector(map(&Foo::getA), v); + CHECK(r == decltype(r){1}); +} + +TEST_CASE("map, pointer to member object") +{ + auto v = std::vector{{1}}; + auto r = into_vector(map(&Foo::a), v); + CHECK(r == decltype(r){1}); +} diff --git a/zug/compat/invoke.hpp b/zug/compat/invoke.hpp index 01bb235..d49405a 100644 --- a/zug/compat/invoke.hpp +++ b/zug/compat/invoke.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include namespace zug { namespace compat { @@ -22,20 +23,20 @@ template inline auto do_invoke(F&& f, Args&&... args) -> ZUG_DETAIL_DECLTYPE_RETURN( std::forward(f)(std::forward(args)...)) -template +template ::value>> inline auto do_invoke(T Base::*pmd, Derived&& ref) -> ZUG_DETAIL_DECLTYPE_RETURN(std::forward(ref).*pmd) -template +template ::value>> inline auto do_invoke(PMD pmd, Pointer&& ptr) -> ZUG_DETAIL_DECLTYPE_RETURN((*std::forward(ptr)).*pmd) -template +template ::value>> inline auto do_invoke(T Base::*pmf, Derived&& ref, Args&&... args) -> ZUG_DETAIL_DECLTYPE_RETURN((std::forward(ref).* pmf)(std::forward(args)...)) -template +template ::value>> inline auto do_invoke(PMF pmf, Pointer&& ptr, Args&&... args) -> ZUG_DETAIL_DECLTYPE_RETURN(((*std::forward(ptr)).* pmf)(std::forward(args)...))