Is it possible to simplify the typedef of a member function with helper template classes?

I recently discovered that the function pointer syntax can be simplified by using the following helper class:

template<typename Sig>
struct Fun {
    typedef Sig* Ptr;
};

It allows me a pointer to the void()following:

typedef Fun<void()>::Ptr fun_ptr;
fun_ptr f = foo;

I would like to create a similar utility for creating typedef to member function pointers. This will allow you to use the following syntax:

struct Foo {
    void bar() {}
};

typedef MemFun<Foo, void()>::Ptr bar_type;
bar_type b = &Foo::bar;

However, I cannot understand the typedef syntax:

template<class T, typename Sig>
struct MemFun {
    // How to use T and Sig to create member function typedef?
};

Can anyone help?

+5
source share
1 answer
template<typename T, typename Sig>
struct convenience {
    typedef Sig T::*type;
};

struct test {
    void member() {}
    void cmember() const {}
};

static_assert( std::is_same<
        convenience<test, void()>::type
        , decltype(&test::member)
    >::value, "Oops" );

static_assert( std::is_same<
        convenience<test, void() const>::type
        , decltype(&test::cmember)
    >::value, "Oops" );

Sig , -, . , , void() const.

+2

All Articles