Boost logo

Boost Users :

Subject: Re: [Boost-users] [mem_fn] return type
From: Lowell McLinskey (lmclinskey_at_[hidden])
Date: 2009-09-01 09:02:07


On Tue, Sep 1, 2009 at 1:57 AM, OvermindDL1 <overminddl1_at_[hidden]> wrote:
>
> On Mon, Aug 31, 2009 at 9:33 PM, Lowell McLinskey<lmclinskey_at_[hidden]> wrote:
> > I am having trouble understanding the return type of mem_fn.  Is it possible
> > to create a vector of this type, analogous to how one can create a vector of
> > type mem_fun_t to hold the return values of mem_fun()?  Here is an example
> > of what I'd like to do.  Thanks in advance.
> > #include <vector>
> > #include <functional>
> > #include "boost/shared_ptr.hpp"
> > #include "boost/mem_fn.hpp"
> > class A
> > {
> > public:
> >     A(const int a) : a_(a) {}
> >     const int get() const {return a_;}
> > private:
> >     int a_;
> > };
> > int main(int argc, char** argv)
> > {
> >     // I can do this
> >     std::vector<std::const_mem_fun_t<const int, A> > v;
> >     v.push_back(std::mem_fun(&A::get));
> >     int i = v[0](new A(4));
> >     // and this
> >     boost::shared_ptr<A> p(new A(7));
> >     int j = boost::mem_fn(&A::get)(p);
> >     // but how do I do this?
> >     // std::vector< WHAT GOES HERE > w;
> >     // w.push_back(boost::mem_fn(&A::get));
> >     // int k = w[0](p);
> >
> >     return 0;
> > }
>
> I do not know if mem_fun supports the boost::result_of template, but
> if it does that would let you get what you need, and if it does not
> support it then that needs to be fixed.

It looks like the documentation indicates that boost::result_of is supported:

unspecified-N::result_type is defined as the return type of the member
function pointer passed as an argument to mem_fn (R in the following):

template<class R, class T> unspecified-2 mem_fn(R (T::*pmf) () const);

I'm still not sure what the template parameter should be to make the
following work:

    typedef boost::function_types::result_type< What Goes Here > result_type;
    std::vector<result_type> w;
    w.push_back(boost::mem_fn(&A::get));
    boost::shared_ptr<A> p(new A(7));
    int k = w[0](p); // k = 7

I was able to deduce from compiler errors something that does actually
work, but it doesn't seem very intuitive:

    boost::shared_ptr<A> p(new A(7));
    std::vector<boost::_mfi::cmf0<const int, A> > u;
    u.push_back(boost::mem_fn(&A::get));
    int n = u[0](p); // n = 7


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net