如何使用 lambda 替换 std::bind

问题描述

auto dis_calculator =
        std::bind(&LaneTrackerImpl::calc_lane_distance,this,std::placeholders::_1,std::placeholders::_2,false,0.0f,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5,std::placeholders::_6);

我这样写代码

 auto lane_distance_calculator = [this](const Lane &lane1,const Lane &lane2,bool disable_lane_start_distance_affection,bool disable_lane_end_distance_affection,distanceMethod method,bool is_matching_phase)
{
    this->calc_lane_distance(
        lane1,lane2,disable_lane_start_distance_affection,disable_lane_end_distance_affection,method,is_matching_phase);
};

和 func merge_lanes 使用它:

merge_lanes(prob_thresh_list_,detected_lanes.lanes,tracked_lanes.lanes,result.lanes,next_track_id,detection_score_threshold_,lane_distance_calculator); 

错误

 error: no matching function for call to ‘{anonymous}::LaneTrackerImpl::merge_lanes(std::vector<float,std::allocator<float> >&,std::vector<perception::Lane>&,size_t&,float&,{anonymous}::LaneTrackerImpl::merge(perception::LanePrediction&,perception::LanePrediction&,size_t)::<lambda(const perception::Lane&,const perception::Lane&,bool,{anonymous}::LaneTrackerImpl::distanceMethod,bool)>&)’
             lane_distance_calculator);

我该如何解决?这让我困惑了很长时间。

解决方法

你很近。试试这个:

auto dis_calculator = [this](const Lane &lane1,const Lane &lane2,bool disable_lane_start_distance_affection,bool disable_lane_end_distance_affection,DistanceMethod method,bool is_matching_phase)
{
    this->calc_lane_distance(
        lane1,lane2,false,0.0f,disable_lane_start_distance_affection,disable_lane_end_distance_affection,method,is_matching_phase);
};