Search code examples
c++rospoint-cloud-libraryros2

How to use lambda intead of std::bind in create_subscription method in ROS2


I am creating a small euclidean clustering node for the point cloud, utilizing PCL's implementation of the KD tree.

Currently, my subscriber definition looks like this,

ClusteringNode::ClusteringNode(const rclcpp::NodeOptions &options)
      : Node("clustering_node", options),
        cloud_subscriber_{create_subscription<PointCloudMsg>(
            "input_cloud", 10,
            std::bind(&ClusteringNode::callback, this,
                      std::placeholders::_1))},

the definition of KD-tree method is inside callback method and is working perfectly,

Now just to improve it furthur, I want to use lambdas instead of the std::bind function here,

What should be the approach here?


Solution

  • The std::bind() statement:

    std::bind(&ClusteringNode::callback, this, std::placeholders::_1)
    

    Would translate into a lambda like this:

    [this](const auto msg){ callback(msg); }
    

    Or, if the callback() has a non-void return value:

    [this](const auto msg){ return callback(msg); }