Skip to content

Use of std::bind in C++ code examples #3618

Closed
@FelipeGdM

Description

@FelipeGdM

Hi everyone!

In the tutorial page Writing a simple publisher and subscriber (C++), the code example provided for the MinimalPublisher uses the C++11 function std::bind at the wall timer creation

timer_ = this->create_wall_timer(
      500ms, std::bind(&MinimalPublisher::timer_callback, this));

In C++14 and later, lambda expressions are found to be preferred as they are easier to write and understand than std::bind. Static analyzers such as clang-tidy and SonarSource consider the use of std::bind as a code smell in C++14 and newer versions

As a personal comment, I found this line of code to be the most difficult to explain to ROS 2 newcomers, as usually students tend to learn ROS 2 at the same time they are learning modern C++. Any improvement of the readability of the Beginner Tutorials may be a huge benefit when teaching ROS 2 to newbies

The ROS 2 cpp examples folder already have a publisher example using lambda expressions, so my question is, is there any reason to keep the std::bind version in the docs page?

If the answer is no, I would gladly update it to use a more readable and modern idiom of C++

Kind regards,
Felipe Gomes de Melo

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions