Boost logo

Boost :

From: Rodolfo Schulz de Lima (rodolfo_at_[hidden])
Date: 2008-03-12 16:56:27


Robert Dailey wrote:

> In the case above, I now have an object bound to my function pointer,
> however I am required to explicitly place a _1, _2, _3 (etc) depending on
> how many parameters the member function takes. Is there a way that this can
> be automated? Thanks.

I did something like that using variadic templates (with gcc-4.3 in
c++0x mode) for calling member functions without specifying the
parameter count.

It's something like this:

template <class T, class U, class R, class... ARGS> class memfun_caller_t
{
public:
    memfun_caller_t(T *ptr, std::function<R(U*,ARGS...)> member)
          : m_ptr(ptr), m_member(member) {}

    R operator()(ARGS&&... args)
    {
       return m_member(m_ptr, std::forward<ARGS>(args)...);
    }

private:
    T *m_ptr;
    std::function<R(U*,ARGS...)> m_member;
};
template <class T, class U, class R, class... ARGS>
memfun_caller_t<T,U,R,ARGS...> bind(R(T::*member)(ARGS...), U *obj)/*{{{*/
{
     return memfun_caller_t<T,U,R,ARGS...>(obj, member);
}

Unfortunately boost::bind didn't help me on this one. If you can't cope
with variadic templates, boost::preprocessor could be used to generate
overloads.

Regards,
rod


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk