传递绑定成员函数

时间:2016-04-08 02:24:50

标签: c++ boost-function

我正在尝试将绑定成员函数传递给例程,并且结果类型由模板类确定:

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;
}

调用顺序是:

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);

无法编译 严重性代码描述项目文件行 错误C2783'std :: vector&gt; testspread :: splat(double,double,int,Fun,double)':无法推断'P'的模板参数

如何确定P?

1 个答案:

答案 0 :(得分:1)

在你的签名

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

您需要(不可扣除)类型P(您不能使用)。

将您的代码更改为

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;
}

应该解决您的问题。