doby
doby

Reputation: 585

pass a bound member function

I am trying to pass a bound member function into a routine and have the result type be determined by the template class:

template< class Fun, class P>
auto splat(double start, double finish, int steps,  Fun fun, double bias)
{
    typedef BOOST_TYPEOF(Fun) Fun_type;
    typedef boost::function_traits<Fun_type> function_traits;
    typedef boost::function_traits<Fun_type>::result_type P;
    vector<P> out = vector<P>(steps);
    double a = steps;
    a = a / 2;
    double step = (finish - start) / (steps - 1);
    double param = start;
    for (int i = 0; i < steps; i++)
    {
        out[i] = fun(param);
        param += step*(1 + accel*(i - a - 1) / (a - 1));
    }
    return out;
}

The calling sequence is:

std::function<BallLib::Point(double)> pFun = std::bind(&BallPath::path, one, _1);
Point ppp = pFun(0.0);
vector<Point> line = testspread::splat(0.0, 3.1415, 10, pFun, 0.0);

It fails to compile with Severity Code Description Project File Line Error C2783 'std::vector> testspread::splat(double,double,int,Fun,double)': could not deduce template argument for 'P'

How do I get it to determine P?

Upvotes: 0

Views: 259

Answers (1)

Jarod42
Jarod42

Reputation: 217145

In your signature

template <class Fun, class P>
auto splat(double start, double finish, int steps, Fun fun, double bias);

You require a (non deducible) type P (that you don't use).

Change your code to

template <class Fun>
auto splat(double start, double finish, int steps, Fun fun, double bias)
{
    typedef BOOST_TYPEOF(Fun) Fun_type;
    typedef boost::function_traits<Fun_type> function_traits;
    typedef boost::function_traits<Fun_type>::result_type P;
    vector<P> out(steps);
    const double a = steps / 2;
    const double step = (finish - start) / (steps - 1);
    double param = start;
    for (int i = 0; i < steps; i++)
    {
        out[i] = fun(param);
        param += step*(1 + accel*(i - a - 1) / (a - 1));
    }
    return out;
}

should fix your issue.

Upvotes: 1

Related Questions