Boost logo

Boost :

From: Sylvain Pion (Sylvain.Pion_at_[hidden])
Date: 2002-11-22 04:48:11


On Thu, Nov 21, 2002 at 11:03:58AM -0500, David Abrahams wrote:
> Do we have any precedent for ways to find out what the arity and
> argument types of an arbitrary function object is (I'm not talking
> about function pointers, here, but "functors")?

There's one in the CGAL library. There, the kernel functors look like :

template <typename K>
class Angle_2
{
    typedef typename K::Point_2 Point_2;
public:
    typedef Angle result_type;
    typedef Arity_tag< 3 > Arity;

    Angle
    operator()(const Point_2& p, const Point_2& q, const Point_2& r) const
    { return angle(p, q, r); }
};

So there is an "Arity" nested type in each functor. When there are several
operators() with different arities, the Arity type is still provided
(so that generic adapters can be used), but then it only acts as a default.
You can change the arity, when the default is not what you want, with an
adapter Set_arity<F, new_arity>.

Access to the arity is supposed to be done with an Arity_traits<>, so that
it can be made to work with std:: functors as well, e.g. :

template < class T >
struct Arity_traits< std::plus< T > > {
  typedef Arity_tag< 2 > Arity;
};

-- 
Sylvain

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