Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2000-10-18 07:23:04


As several others have posted, there's been some work on the subject.
There's also a "deref-iterator" of some kind floating around (that just adds
an extra dereference when dereferenced); that's what I use to solve this
kind of problem. Any of the already-mentioned solutions remove the need for
adapters for function objects.

But to answer your original question:

> What I'm not wild about is having to explicitly provide the type to the
> template (what happened to template-argument deduction? Does it not apply
> here?).

You need a function for template argument deduction; the constructor isn't
enough:

> template<typename _Pred> struct pointer_proxy2 :
> std::binary_function<typename _Pred::result_type, typename
> _Pred::first_argument_type, typename _Pred::second_argument_type>
> {
> _Pred Pr;
> pointer_proxy2(_Pred pr) : Pr(pr) {}
> inline typename _Pred::result_type operator()(const typename
> _Pred::first_argument_type *f, const typename _Pred::second_argument_type
> *s) const {
> return Pr(*f, *s);
> }
> };

Add this "constructor function":

template <typename _Pred> pointer_proxy2<_Pred> make_proxy2(const _Pred &
pr)
{ return pointer_proxy2<_Pred>(pr); }

Then,
  std::sort(v.begin(), v.end(),
      pointer_proxy2<std::less<int> >(std::less<int>()));
becomes:
  std::sort(v.begin(), v.end(),
      make_proxy2(std::less<int>()) );

Also see std::pair/make_pair, unary_negate/not1, binder1st/bind1st, etc. for
more examples of the same pattern in the Standard. All of the function
object/pointer adapters in the Standard use a similar pairing.

        -Steve


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