【问题标题】:How can I replace std::bind using lambda如何使用 lambda 替换 std::bind
【发布时间】:2021-05-14 14:47:23
【问题描述】:
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, false, 0.0f,
        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>&, std::vector<perception::Lane>&, std::vector<perception::Lane>&, size_t&, float&, {anonymous}::LaneTrackerImpl::merge(perception::LanePrediction&, perception::LanePrediction&, size_t)::<lambda(const perception::Lane&, const perception::Lane&, bool, bool, {anonymous}::LaneTrackerImpl::DistanceMethod, bool)>&)’
             lane_distance_calculator);

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

【问题讨论】:

  • 我在您提供的代码中没有看到merge_lanes
  • 另外,我没有看到结束的 '}'。
  • 如果我们不考虑缺少的},您发布的代码是正确的。我认为你得到的错误来自代码中的其他地方,更具体地说是你在 lambda 中添加的内容。

标签: c++ lambda stdbind


【解决方案1】:

你很接近。试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多