Is it possible to extract parameter types from a template <auto MEMFN>?

好久不见. 提交于 2019-12-13 08:46:26

问题


Is it possible to create a standalone template function which has a template parameter auto MEMFN (a member function pointer), and has the same return and parameter types as MEMFN has?

So, if MEMFN's type is

RETURN (OBJECT::*)(PARAMETERS...)

then the desired function is this:

template <auto MEMFN>
RETURN foo(OBJECT &, PARAMETERS...);

My problem is how to extract PARAMETERS... from MEMFN's type (RETURN and OBJECT are easy to do).

So I can call this function like this:

Object o;
foo<&Object::func>(o, <parameters>...);

As for request from n.m., here is a stripped down example from an actual code:

#include <utility>

template <typename RETURN, typename OBJECT, typename ...PARAMETERS>
struct Wrapper {
    template <RETURN (OBJECT::*MEMFN)(PARAMETERS...)>
    RETURN foo(PARAMETERS... parameters) {
        // do whatever with MEMFN, parameters, etc. here, not part of the problem
    }
};

struct Object {
    template <auto MEMFN, typename RETURN, typename OBJECT, typename ...PARAMETERS>
    RETURN call(OBJECT &&object, PARAMETERS &&...parameters) {
        // here, MEMFN parameters and PARAMETERS must be the same

        // Wrapper actually not created here, it is accessed by other means
        Wrapper<RETURN, typename std::decay<OBJECT>::type, PARAMETERS...> w;

        return w.template foo<MEMFN>(std::forward<PARAMETERS>(parameters)...);
    }
};

struct Foo {
    void fn(int);
};

int main() {
    Object o;
    Foo f;
    o.call<&Foo::fn, void, Foo &, int>(f, 42);

    // this is wanted instead:
    // o.call<&Foo::fn>(f, 42);
}

回答1:


If you relax your demand on being standalone you can do something like:

#include <iostream>

template <auto MEMFN, class = decltype(MEMFN)>
struct S;

template <auto MEMFN, class Ret, class T, class... Args>
struct S<MEMFN, Ret (T::*)(Args...)> {
    static Ret foo(T &o, Args... args) {
        (o.*MEMFN)(args...);
    }
};

struct A {
    void foo(int a, int b) {
        std::cout << a << " " << b << std::endl;
    }
};

int main() {
    A a;
    S<&A::foo>::foo(a, 1, 2);
}

[live demo]


If no then you gonna have to have a patience to create a function overloads for each possible number of parameters:

#include <type_traits>
#include <tuple>
#include <iostream>

template <class, std::size_t>
struct DeduceParam;

template <class Ret, class T, class... Args, std::size_t N>
struct DeduceParam<Ret (T::*)(Args...), N> {
    using type = std::tuple_element_t<N, std::tuple<Args...>>;
};

template <class>
struct DeduceResultAndType;

template <class Ret, class T, class... Args>
struct DeduceResultAndType<Ret (T::*)(Args...)> {
    using result = Ret;
    using type = T;
    static constexpr decltype(sizeof(T)) size = sizeof...(Args);
};

template <auto MEMFN, class DRAT = DeduceResultAndType<decltype(MEMFN)>, std::enable_if_t<DRAT::size == 1>* = nullptr>
typename DRAT::result  foo(typename DRAT::type o, typename DeduceParam<decltype(MEMFN), 0>::type param1) {
}

template <auto MEMFN, class DRAT = DeduceResultAndType<decltype(MEMFN)>, std::enable_if_t<DRAT::size == 2>* = nullptr>
typename DRAT::result  foo(typename DRAT::type o, typename DeduceParam<decltype(MEMFN), 0>::type param1, 
                                                  typename DeduceParam<decltype(MEMFN), 1>::type param2) {
}

struct A {
    void foo(int a, int b) {
        std::cout << a << " " << b << std::endl;
    }
};

int main() {
    A a;
    foo<&A::foo>(a, 1, 2);
}



回答2:


Yes we can:

template <auto MemFn>
struct fooHelper;

template <typename Ret, typename Obj, typename ... Args, Ret (Obj::*MemFn)(Args...)>
struct fooHelper<MemFn>
{
    static Ret call(Obj& obj, Args... args) {
        return (obj.*MemFn)(args...);
    }
};

template <auto MemFn, typename ... Args>
auto foo(Args ... args)
{
    return fooHelper<MemFn>::call(args...);
}

Another way to define foo which doesn't introduce a brand new parameter pack is:

template <auto MemFn>
auto& foo = fooHelper<MemFn>::call;

Example usage:

#include <iostream>

struct moo
{
    int doit (int x, int y) { return x + y; }
};

int main()
{
    moo m;
    std::cout << foo<&moo::doit>(m, 1, 2) << "\n";
}

(Perfect forwarding omitted for simplicity)



来源:https://stackoverflow.com/questions/46405560/is-it-possible-to-extract-parameter-types-from-a-template-auto-memfn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!