Skip to content

Fix non-const shared_ptr callback deprecation warnings with GCC - #3450

Open
christophebedard wants to merge 1 commit into
rollingfrom
christophebedard/fix-callback-deprecation-warnings
Open

christophebedard wants to merge 1 commit into
rollingfrom
christophebedard/fix-callback-deprecation-warnings

Conversation

@christophebedard

@christophebedard christophebedard commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Description

Subscription callbacks taking a std::shared_ptr to a non-const message were deprecated in #2975 (Lyrical), but the [[deprecated]] function, AnySubscriptionCallback::set_deprecated(), is only used inside rclcpp headers. Downstream packages include these as system headers, and GCC does not show warnings located in system headers, even if user code triggered the template instantiation, so only Clang >= 18 shows the warning.

Instead, add [[deprecated]] overloads of create_subscription() and create_generic_subscription() (free functions and Node and LifecycleNode member functions), constrained to the deprecated callback signatures, so that the warning is located at the user's call. Then remove set_deprecated() and the RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS macro.

Since GCC users will only see the warning starting with Makoa, add TODOs to remove these overloads and the support for these callback signatures after Makoa.

Part of #3176

Is this user-facing behavior change?

Yes: with GCC, subscription callbacks taking a std::shared_ptr<MessageT> (e.g., MessageT::SharedPtr) now trigger a deprecation warning at the create_subscription() or create_generic_subscription() call. Use std::shared_ptr<const MessageT> (e.g., MessageT::ConstSharedPtr) instead. With Clang, the warning is now also located at that call, but it is shown twice for the Node and LifecycleNode member functions (once for the function template and once for its specialization).

Example with GCC 15
/tmp/dws/src/my_package/src/listener.cpp: In constructor ‘Listener::Listener()’:
/tmp/dws/src/my_package/src/listener.cpp:12:63: warning: ‘std::shared_ptr<ROSMessageT> rclcpp::Node::create_subscription(const std::string&, const rclcpp::QoS&, CallbackT&&, const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT>&, typename MessageMemoryStrategyT::SharedPtr) [with MessageT = std_msgs::msg::String_<std::allocator<void> >; CallbackT = Listener::Listener()::<lambda(std_msgs::msg::String_<std::allocator<void> >::SharedPtr)>; AllocatorT = std::allocator<void>; SubscriptionT = rclcpp::Subscription<std_msgs::msg::String_<std::allocator<void> > >; MessageMemoryStrategyT = rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String_<std::allocator<void> >, std::allocator<void> >; std::string = std::__cxx11::basic_string<char>; typename MessageMemoryStrategyT::SharedPtr = std::shared_ptr<rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String_<std::allocator<void> >, std::allocator<void> > >]’ is deprecated: subscription callbacks taking a std::shared_ptr to a non-const message are deprecated, use std::shared_ptr<const MessageT> instead [-Wdeprecated-declarations]
   12 |     subscription_ = create_subscription<std_msgs::msg::String>(
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
   13 |       "topic", 10,
      |       ~~~~~~~~~~~~
   14 |       [this](std_msgs::msg::String::SharedPtr msg) {
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   15 |         RCLCPP_INFO(get_logger(), "I heard: '%s'", msg->data.c_str());
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   16 |       });
      |       ~~
In file included from /ws/install/rclcpp/include/rclcpp/rclcpp/node.hpp:1760,
                 from /ws/install/rclcpp/include/rclcpp/rclcpp/executors.hpp:25,
                 from /ws/install/rclcpp/include/rclcpp/rclcpp/rclcpp.hpp:177,
                 from /tmp/dws/src/my_package/src/listener.cpp:3:
/ws/install/rclcpp/include/rclcpp/rclcpp/node_impl.hpp:109:1: note: declared here
  109 | Node::create_subscription(
      | ^~~~

Calling AnySubscriptionCallback::set() directly with a deprecated callback no longer triggers a warning (it previously did only with Clang).

Did you use Generative AI?

Claude Opus 5.5 to find out why the warning was not shown with GCC and to implement this fix.

Additional Information

This came up in #3449, which removes these callbacks and should now wait until after Makoa. With this, GCC shows the warning in ros-tooling/topic_tools, which ros-tooling/topic_tools#159 fixes.

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

ABI Compliance Check

✅ Verdict: compatible (additions only)

Library Verdict Summary
libcomponent_manager.so ✅ compatible No ABI changes detected.
librclcpp.so ✅ additions-only ABI changed but only with additions (backward-compatible).
librclcpp_action.so ✅ compatible No ABI changes detected.
librclcpp_lifecycle.so ✅ compatible No ABI changes detected.
✅ libcomponent_manager.so — full abidiff report

Compared:

  • Base: lib-base/libcomponent_manager.so
  • Head: lib-pr/libcomponent_manager.so @ 8bf189c
(empty report — no differences printed by abidiff)
✅ librclcpp.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp.so
  • Head: lib-pr/librclcpp.so @ 8bf189c
Functions changes summary: 0 Removed, 0 Changed, 14 Added (2 filtered out) functions
Variables changes summary: 0 Removed, 0 Changed, 0 Added variable

14 Added functions:

  [A] 'method rclcpp::ClientBase::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_client_by_handle(const std::shared_ptr<constrcl_client_s>&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy20get_client_by_handleERKSt10shared_ptrIK12rcl_client_sERKSt3mapISt8weak_ptrINS_13CallbackGroupEES9_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISB_ESaISt4pairIKSB_SE_EEE}
  [A] 'method rclcpp::CallbackGroup::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_group_by_client(const rclcpp::ClientBase::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy19get_group_by_clientERKSt10shared_ptrINS_10ClientBaseEERKSt3mapISt8weak_ptrINS_13CallbackGroupEES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISA_ESaISt4pairIKSA_SD_EEE}
  [A] 'method rclcpp::CallbackGroup::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_group_by_service(const rclcpp::ServiceBase::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy20get_group_by_serviceERKSt10shared_ptrINS_11ServiceBaseEERKSt3mapISt8weak_ptrINS_13CallbackGroupEES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISA_ESaISt4pairIKSA_SD_EEE}
  [A] 'method rclcpp::CallbackGroup::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_group_by_subscription(const rclcpp::SubscriptionBase::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy25get_group_by_subscriptionERKSt10shared_ptrINS_16SubscriptionBaseEERKSt3mapISt8weak_ptrINS_13CallbackGroupEES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISA_ESaISt4pairIKSA_SD_EEE}
  [A] 'method rclcpp::CallbackGroup::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_group_by_timer(const rclcpp::TimerBase::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy18get_group_by_timerERKSt10shared_ptrINS_9TimerBaseEERKSt3mapISt8weak_ptrINS_13CallbackGroupEES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISA_ESaISt4pairIKSA_SD_EEE}
  [A] 'method rclcpp::CallbackGroup::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_group_by_waitable(const rclcpp::Waitable::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy21get_group_by_waitableERKSt10shared_ptrINS_8WaitableEERKSt3mapISt8weak_ptrINS_13CallbackGroupEES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISA_ESaISt4pairIKSA_SD_EEE}
  [A] 'method rclcpp::node_interfaces::NodeBaseInterface::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_node_by_group(const rclcpp::CallbackGroup::SharedPtr&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy17get_node_by_groupERKSt10shared_ptrINS_13CallbackGroupEERKSt3mapISt8weak_ptrIS3_ES8_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessIS9_ESaISt4pairIKS9_SC_EEE}
  [A] 'method rclcpp::ServiceBase::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_service_by_handle(const std::shared_ptr<constrcl_service_s>&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy21get_service_by_handleERKSt10shared_ptrIK13rcl_service_sERKSt3mapISt8weak_ptrINS_13CallbackGroupEES9_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISB_ESaISt4pairIKSB_SE_EEE}
  [A] 'method rclcpp::SubscriptionBase::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_subscription_by_handle(const std::shared_ptr<constrcl_subscription_s>&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy26get_subscription_by_handleERKSt10shared_ptrIK18rcl_subscription_sERKSt3mapISt8weak_ptrINS_13CallbackGroupEES9_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISB_ESaISt4pairIKSB_SE_EEE}
  [A] 'method rclcpp::TimerBase::SharedPtr rclcpp::memory_strategy::MemoryStrategy::get_timer_by_handle(const std::shared_ptr<constrcl_timer_s>&, const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap&)'    {_ZN6rclcpp15memory_strategy14MemoryStrategy19get_timer_by_handleERKSt10shared_ptrIK11rcl_timer_sERKSt3mapISt8weak_ptrINS_13CallbackGroupEES9_INS_15node_interfaces17NodeBaseInterfaceEESt10owner_lessISB_ESaISt4pairIKSB_SE_EEE}
  [A] 'function void rclcpp::spin_all(const rclcpp::Node::SharedPtr&, std::chrono::nanoseconds)'    {_ZN6rclcpp8spin_allERKSt10shared_ptrINS_4NodeEENSt6chrono8durationIlSt5ratioILl1ELl1000000000EEEE}
  [A] 'function void rclcpp::spin_all(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr&, std::chrono::nanoseconds)'    {_ZN6rclcpp8spin_allERKSt10shared_ptrINS_15node_interfaces17NodeBaseInterfaceEENSt6chrono8durationIlSt5ratioILl1ELl1000000000EEEE}
  [A] 'function void rclcpp::spin_some(const rclcpp::Node::SharedPtr&)'    {_ZN6rclcpp9spin_someERKSt10shared_ptrINS_4NodeEE}
  [A] 'function void rclcpp::spin_some(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr&)'    {_ZN6rclcpp9spin_someERKSt10shared_ptrINS_15node_interfaces17NodeBaseInterfaceEE}


✅ librclcpp_action.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp_action.so
  • Head: lib-pr/librclcpp_action.so @ 8bf189c
(empty report — no differences printed by abidiff)
✅ librclcpp_lifecycle.so — full abidiff report

Compared:

  • Base: lib-base/librclcpp_lifecycle.so
  • Head: lib-pr/librclcpp_lifecycle.so @ 8bf189c
(empty report — no differences printed by abidiff)

Updated for commit 8bf189c · check run · suppressions: /home/runner/work/_temp/ros2-abi-suppressions.txt

Subscription callbacks taking a `std::shared_ptr` to a non-const
message were deprecated in #2975
(Lyrical), but the `[[deprecated]]` function,
`AnySubscriptionCallback::set_deprecated()`, is only used inside rclcpp
headers. Downstream packages include these as system headers, and GCC
does not show warnings located in system headers, even if user code
triggered the template instantiation, so only Clang >= 18 shows the
warning.

Instead, add `[[deprecated]]` overloads of `create_subscription()` and
`create_generic_subscription()` (free functions and `Node` and
`LifecycleNode` member functions), constrained to the deprecated
callback signatures, so that the warning is located at the user's call.
Move the `create_generic_subscription()` implementation to
`rclcpp::detail` so that both overloads can use it, like
`create_subscription()`. Then remove `set_deprecated()` and the
`RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS` macro.

Since GCC users will only see the warning starting with Makoa, add TODOs
to remove these overloads and the support for these callback signatures
after Makoa.

Also update the `rclcpp_action` tests that used a deprecated callback.

Part of #3176

Signed-off-by: Christophe Bedard <[email protected]>
@christophebedard
christophebedard force-pushed the christophebedard/fix-callback-deprecation-warnings branch from 3fe4dbd to 8bf189c Compare November 28, 2026 00:24

@mini-1235 mini-1235 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some tests and this works for me! My only concern is that it adds quite a few lines of code, but I am not sure how to better handle the GCC situation, and this will eventually be removed anyway.

@christophebedard

Copy link
Copy Markdown
Member Author

Pulls: #3450
Gist: https://gist.githubusercontent.com/christophebedard/3bbba8566731091e87209b5a191e0452/raw/e12742d69c8aa25ab5acbc785902f6f99077be9e/ros2.repos
BUILD args: --packages-above-and-dependencies rclcpp rclcpp_lifecycle rclcpp_action
TEST args: --packages-above rclcpp rclcpp_lifecycle rclcpp_action
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/21367

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants