|
Boost Users : |
Subject: [Boost-users] [function_traits] How to detect function arity for functors with member template?
From: jesseperla (jesseperla_at_[hidden])
Date: 2009-07-27 09:23:06
I am interested in tag dispatching in a generic function depending on
whether the passed in argument is arity 1 or 2.
My problem is that I want to have a generic expectation operator that
is passed in a function to take the expectation over, and a
distribution for the integration. The expectation needs to support
both real and mixed-integer measures and functions. Consider the
following functions it should work with:
struct my_real_functor
{
template<std::size_t N>
double operator()(ublas::bounded_vector<double,N>& x) {...}
};
template<std::size_t N>
double my_real_function(ublas::bounded_vector<double,N>& x) {...}
struct my_mixed_functor
{
template<std::size_t RealN, std::size_t IntN>
double operator()(ublas::bounded_vector<double,RealN>& real_x,
ublas::bounded_vector<int,RealN>& int_x) {...}
};
.... naturally, part of the problem is that the functor could have
both specializations,
so I recognize this might be tricky. If both are on a functor, then I
can default to
one of the implementations(probably the unary) for the matching.
---------------------------------------------------
For the operator itself, the signature would be something like.
<class F, class Distribution>
double expectation(F f, const Distribution& dist)
{
.....// tag dispatch depending on F arity, or something similar.
}
..... or even better:
<class F, class Distribution>
double expectation(mpl::enable_if<has_unary,F>::type f, const
Distribution& dist)
{
.....//unary implementation
}
<class F, class Distribution>
double expectation(mpl::enable_if<has_binary,F>::type f, const
Distribution& dist)
{
.....// binary implementation.
}
.... or even better if I could fully match the signatures of the
passed in F for conformance.
------------------------------------------------------
I looked at boost function_traits but it doesn't work with member
functors....
http://www.boost.org/doc/libs/1_39_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html
Any ideas on how best to implement this?
Thanks as always,
Jesse
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