ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

::SharedPtr vs std::shared_ptr<>

asked 2022-11-23 14:45:32 -0600

jrtg gravatar image

Hello,

Please bear with me, I haven´t been programming since 2006, and it turns out that learning both c++11/c++17 and ROS2 has a bit of a steep learning curve. ;-)

After reading the relevant chapters at learncpp.com, I think I have an adequate understanding of std::shared_ptr, weak_ptr and unique_ptr.

However, going back to the ROS2 tutorials, I see that often an alias is defined:

using something::SharedPtr = std::shared_ptr<something>

For instance, in the publisher tutorial, the publisher is declared as follows:

rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;

which (I think) is fully equivalent to:

std::shared_ptr<clcpp::Publisher<std_msgs::msg::String>> publisher_;

I find this rather confusing, even more so since it is not general. For instance, in the TF2 TransformBroadcaster tutorial, the broadcaster must be declared as follows:

std::shared_ptr<tf2_ros::StaticTransformBroadcaster> tf_static_broadcaster_;

since the ::SharedPtr alias is not defined:

tf2_ros::StaticTransformBroadcaster::SharedPtr tf_static_broadcaster_;
// Not ok: "class tf2_ros::StaticTransformBroadcaster has no member SharedPtr"

So: is there any specific reason (other than it being 6 characters less) to give preference to the ::SharedPtr way of declaring, or is it ok to stick to using std::shared_ptr<...>?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-11-24 08:24:01 -0600

Mike Scheutzow gravatar image

It is certainly fine to use std::shared_ptr<> for new code.

The SharedPtr alias is a legacy from the ros1 days. std::shared_ptr<> is a relatively recent addition to the c++ standard, and before that the SharedPtr defined by ros was a type alias to a boost library shared pointer class. It saved a lot more typing back then and made it clearer what the code was doing.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-11-23 07:05:14 -0600

Seen: 1,025 times

Last updated: Nov 24 '22