Boost logo

Boost Users :

Subject: [Boost-users] Question on std::binary_function template arguments
From: John M. Dlugosz (mpbecey7gu_at_[hidden])
Date: 2011-09-12 21:33:41

You may recall my earlier question asking if there was an easy way to "bind" a member
function to a weak_ptr. Looking at the lower-level STL features, I came up with this
idea, to prepare an adaptor in the same manner as mem_fun and mem_fun_ref. Generalizing
the idea, it takes a member function and produces a functor that takes the object as a
first argument, for various ways of presenting the object. I extended it with
mem_fun_weak, where the resulting functor is called with a weak_ptr as the first argument.

Below is one form (the one I need). To work along with the other STL primitives, there
are a total of 4 forms (const/non-const, one arg or no args). That doesn't have arbitrary
signatures like the more general "bind", which is something I'd like to discuss further.

For now, my question concerns the first template argument to std::binary_function.
Looking at the declarations for the standard mem_fun1_t and mem_fun1_ref_t, I don't know
if this should be:
     1) the full type passed as the first argument to the resulting functor,
     2) such, but without the final &,
     3) just the object's type,
     4..n) something else?

Can someone clarify that for me?

Here is my code (using #1 above):

template <typename Result, typename Type, typename Arg>
class mem_fun1_weak_t : public std::binary_function<const std::tr1::weak_ptr<Type>&, Arg,
Result> {
     Result (Type::*p )( Arg );
    explicit mem_fun1_weak_t( Result (Type::*Pm )( Arg ) ) : p(Pm) {}
    Result operator() (const std::tr1::weak_ptr<Type>& left, Arg right) const
       std::tr1::shared_ptr<Type> sp (left.lock());
       if (sp) {
          Type* object= sp.get(); // shared_ptr doesn't overload operator->*.
          return (object->*p)(right);
       else return Result();

template<typename Result, typename Type, typename Arg>
mem_fun1_weak_t<Result, Type, Arg> mem_fun_weak (Result ( Type::*Pm )( Arg ) )
  return mem_fun1_weak_t<Result, Type, Arg> (Pm);


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at