From 7993966419b5131f8ebf53d23b3832ea6a9c2663 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 26 Nov 2026 14:48:36 -0800 Subject: [PATCH 1/2] Remove deprecated non-const shared_ptr subscription callbacks Subscription callbacks taking a `std::shared_ptr` to a non-const message (`MessageT`, the ROS message type of a type adapter, or `rclcpp::SerializedMessage`), with or without `rclcpp::MessageInfo`, have been deprecated since https://github.com/ros2/rclcpp/pull/2975 (Lyrical) in favor of the `std::shared_ptr` signatures. Remove them, as well as the `RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS` macro and the tests for these signatures, and update the `rclcpp_action` tests that still used one. Without the variant alternatives, these callbacks would silently be dispatched as `std::unique_ptr` callbacks, so reject them with a `static_assert` instead. This also covers `const std::shared_ptr &` callbacks, which were previously rejected as ambiguous. Signed-off-by: Christophe Bedard --- .../rclcpp/any_subscription_callback.hpp | 191 ++++-------------- .../subscription_callback_type_helper.hpp | 31 --- .../rclcpp/test_any_subscription_callback.cpp | 76 ------- rclcpp_action/test/test_client.cpp | 2 +- rclcpp_action/test/test_generic_client.cpp | 2 +- 5 files changed, 39 insertions(+), 263 deletions(-) diff --git a/rclcpp/include/rclcpp/any_subscription_callback.hpp b/rclcpp/include/rclcpp/any_subscription_callback.hpp index d66d7b7cab..e7666043c8 100644 --- a/rclcpp/include/rclcpp/any_subscription_callback.hpp +++ b/rclcpp/include/rclcpp/any_subscription_callback.hpp @@ -137,22 +137,6 @@ struct AnySubscriptionCallbackPossibleTypes std::function &, const rclcpp::MessageInfo &)>; - - // Deprecated signatures: - using SharedPtrCallback = - std::function)>; - using SharedPtrROSMessageCallback = - std::function)>; - using SharedPtrWithInfoCallback = - std::function, const rclcpp::MessageInfo &)>; - using SharedPtrWithInfoROSMessageCallback = - std::function, - const rclcpp::MessageInfo &)>; - using SharedPtrSerializedMessageCallback = - std::function)>; - using SharedPtrSerializedMessageWithInfoCallback = - std::function, const rclcpp::MessageInfo &)>; }; /// Template helper to select the variant type based on whether or not MessageT is a TypeAdapter. @@ -186,11 +170,7 @@ struct AnySubscriptionCallbackHelper typename CallbackTypes::ConstRefSharedConstPtrCallback, typename CallbackTypes::ConstRefSharedConstPtrWithInfoCallback, typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageCallback, - typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback, - typename CallbackTypes::SharedPtrCallback, - typename CallbackTypes::SharedPtrWithInfoCallback, - typename CallbackTypes::SharedPtrSerializedMessageCallback, - typename CallbackTypes::SharedPtrSerializedMessageWithInfoCallback + typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback >; }; @@ -224,13 +204,7 @@ struct AnySubscriptionCallbackHelper typename CallbackTypes::ConstRefSharedConstPtrWithInfoCallback, typename CallbackTypes::ConstRefSharedConstPtrWithInfoROSMessageCallback, typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageCallback, - typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback, - typename CallbackTypes::SharedPtrCallback, - typename CallbackTypes::SharedPtrROSMessageCallback, - typename CallbackTypes::SharedPtrWithInfoCallback, - typename CallbackTypes::SharedPtrWithInfoROSMessageCallback, - typename CallbackTypes::SharedPtrSerializedMessageCallback, - typename CallbackTypes::SharedPtrSerializedMessageWithInfoCallback + typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback >; }; @@ -248,9 +222,7 @@ struct AnySubscriptionCallbackHelper typename CallbackTypes::SharedConstPtrSerializedMessageCallback, typename CallbackTypes::SharedConstPtrSerializedMessageWithInfoCallback, typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageCallback, - typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback, - typename CallbackTypes::SharedPtrSerializedMessageCallback, - typename CallbackTypes::SharedPtrSerializedMessageWithInfoCallback + typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback >; }; @@ -339,18 +311,6 @@ class AnySubscriptionCallback typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageCallback; using ConstRefSharedConstPtrSerializedMessageWithInfoCallback = typename CallbackTypes::ConstRefSharedConstPtrSerializedMessageWithInfoCallback; - using SharedPtrCallback = - typename CallbackTypes::SharedPtrCallback; - using SharedPtrROSMessageCallback = - typename CallbackTypes::SharedPtrROSMessageCallback; - using SharedPtrWithInfoCallback = - typename CallbackTypes::SharedPtrWithInfoCallback; - using SharedPtrWithInfoROSMessageCallback = - typename CallbackTypes::SharedPtrWithInfoROSMessageCallback; - using SharedPtrSerializedMessageCallback = - typename CallbackTypes::SharedPtrSerializedMessageCallback; - using SharedPtrSerializedMessageWithInfoCallback = - typename CallbackTypes::SharedPtrSerializedMessageWithInfoCallback; public: explicit @@ -378,9 +338,9 @@ class AnySubscriptionCallback /// Generic function for setting the callback. /** - * There are specializations that overload this in order to deprecate some - * callback signatures, and also to fix ambiguity between shared_ptr and - * unique_ptr callback signatures when using them with lambda functions. + * There are specializations that overload this in order to fix ambiguity + * between shared_ptr and unique_ptr callback signatures when using them with + * lambda functions. */ template AnySubscriptionCallback @@ -392,77 +352,26 @@ class AnySubscriptionCallback // converted to one another, e.g. shared_ptr and unique_ptr. using scbth = detail::SubscriptionCallbackTypeHelper; - // Determine if the given CallbackT is a deprecated signature or not. - constexpr auto is_deprecated = - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function)> - >::value || - rclcpp::function_traits::same_arguments< - typename scbth::callback_type, - std::function, const rclcpp::MessageInfo &)> - >::value; + using CallbackMessageT = std::decay_t< + typename rclcpp::function_traits::function_traits< + typename scbth::callback_type>::template argument_type<0>>; + constexpr bool is_shared_ptr_to_non_const_message = + std::is_same>::value || + std::is_same>::value || + std::is_same>::value; + static_assert( + !is_shared_ptr_to_non_const_message, + "subscription callbacks taking a std::shared_ptr to a non-const message are not " + "supported, use std::shared_ptr instead"); // Use the discovered type to force the type of callback when assigning // into the variant. - if constexpr (is_deprecated) { - // If deprecated, call sub-routine that is deprecated. - set_deprecated(static_cast(callback)); - } else { - // Otherwise just assign it. - callback_variant_ = static_cast(callback); - } + callback_variant_ = static_cast(callback); // Return copy of self for easier testing, normally will be compiled out. return *this; } - /// Function for shared_ptr to non-const MessageT, which is deprecated. - template - // *INDENT-OFF* - #if !defined(RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS) - // suppress deprecation warnings in `test_any_subscription_callback.cpp` - [[deprecated("use 'void(std::shared_ptr)' instead")]] - #endif - // *INDENT-ON* - void - set_deprecated(std::function)> callback) - { - callback_variant_ = callback; - } - - /// Function for shared_ptr to non-const MessageT with MessageInfo, which is deprecated. - template - // *INDENT-OFF* - #if !defined(RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS) - // suppress deprecation warnings in `test_any_subscription_callback.cpp` - [[deprecated( - "use 'void(std::shared_ptr, const rclcpp::MessageInfo &)' instead" - )]] - #endif - // *INDENT-ON* - void - set_deprecated(std::function, const rclcpp::MessageInfo &)> callback) - { - callback_variant_ = callback; - } - /// Disable the callback from being called during dispatch. void disable() { @@ -584,16 +493,14 @@ class AnySubscriptionCallback } else if constexpr ( // NOLINT[readability/braces] is_ta && ( std::is_same_v|| - std::is_same_v|| - std::is_same_v + std::is_same_v )) { callback(convert_ros_message_to_custom_type_unique_ptr(*message)); } else if constexpr ( // NOLINT[readability/braces] is_ta && ( std::is_same_v|| - std::is_same_v|| - std::is_same_v + std::is_same_v )) { callback(convert_ros_message_to_custom_type_unique_ptr(*message), message_info); @@ -609,14 +516,12 @@ class AnySubscriptionCallback callback(create_ros_unique_ptr_from_ros_shared_ptr_message(message), message_info); } else if constexpr ( // NOLINT[readability/braces] std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { callback(message); } else if constexpr ( // NOLINT[readability/braces] std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { callback(message, message_info); } @@ -629,9 +534,7 @@ class AnySubscriptionCallback std::is_same_v|| std::is_same_v|| std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { throw std::runtime_error( "Cannot dispatch std::shared_ptr message " @@ -681,14 +584,12 @@ class AnySubscriptionCallback message_info); } else if constexpr ( // NOLINT[readability/braces] std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { callback(create_serialized_message_unique_ptr_from_shared_ptr(serialized_message)); } else if constexpr ( // NOLINT[readability/braces] std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { callback( create_serialized_message_unique_ptr_from_shared_ptr(serialized_message), @@ -711,11 +612,7 @@ class AnySubscriptionCallback std::is_same_v|| std::is_same_v|| std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { throw std::runtime_error( "cannot dispatch rclcpp::SerializedMessage to " @@ -759,15 +656,13 @@ class AnySubscriptionCallback callback(*message, message_info); } else if constexpr ( // NOLINT[readability/braces] is_ta && ( - std::is_same_v|| - std::is_same_v + std::is_same_v )) { callback(create_custom_unique_ptr_from_custom_shared_ptr_message(message)); } else if constexpr ( // NOLINT[readability/braces] is_ta && ( - std::is_same_v|| - std::is_same_v + std::is_same_v )) { callback(create_custom_unique_ptr_from_custom_shared_ptr_message(message), message_info); @@ -802,8 +697,7 @@ class AnySubscriptionCallback callback(*message, message_info); } } else if constexpr ( // NOLINT[readability/braces] - std::is_same_v|| - std::is_same_v) + std::is_same_v) { if constexpr (is_ta) { callback(convert_custom_type_to_ros_message_unique_ptr(*message)); @@ -811,8 +705,7 @@ class AnySubscriptionCallback callback(create_ros_unique_ptr_from_ros_shared_ptr_message(message)); } } else if constexpr ( // NOLINT[readability/braces] - std::is_same_v|| - std::is_same_v) + std::is_same_v) { if constexpr (is_ta) { callback(convert_custom_type_to_ros_message_unique_ptr(*message), message_info); @@ -847,9 +740,7 @@ class AnySubscriptionCallback std::is_same_v|| std::is_same_v|| std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { throw std::runtime_error( "Cannot dispatch std::shared_ptr message " @@ -898,14 +789,12 @@ class AnySubscriptionCallback callback(*message, message_info); } else if constexpr ( // NOLINT[readability/braces] is_ta && ( - std::is_same_v|| - std::is_same_v)) + std::is_same_v)) { callback(std::move(message)); } else if constexpr ( // NOLINT[readability/braces] is_ta && ( - std::is_same_v|| - std::is_same_v + std::is_same_v )) { callback(std::move(message), message_info); @@ -940,8 +829,7 @@ class AnySubscriptionCallback callback(*message, message_info); } } else if constexpr ( // NOLINT[readability/braces] - std::is_same_v|| - std::is_same_v) + std::is_same_v) { if constexpr (is_ta) { callback(convert_custom_type_to_ros_message_unique_ptr(*message)); @@ -949,8 +837,7 @@ class AnySubscriptionCallback callback(std::move(message)); } } else if constexpr ( // NOLINT[readability/braces] - std::is_same_v|| - std::is_same_v) + std::is_same_v) { if constexpr (is_ta) { callback(convert_custom_type_to_ros_message_unique_ptr(*message), message_info); @@ -985,9 +872,7 @@ class AnySubscriptionCallback std::is_same_v|| std::is_same_v|| std::is_same_v|| - std::is_same_v|| - std::is_same_v|| - std::is_same_v) + std::is_same_v) { throw std::runtime_error( "Cannot dispatch std::unique_ptr message " @@ -1023,13 +908,11 @@ class AnySubscriptionCallback std::holds_alternative(callback_variant_) || std::holds_alternative(callback_variant_) || std::holds_alternative(callback_variant_) || - std::holds_alternative(callback_variant_) || std::holds_alternative(callback_variant_) || std::holds_alternative(callback_variant_) || std::holds_alternative(callback_variant_) || std::holds_alternative( - callback_variant_) || - std::holds_alternative(callback_variant_); + callback_variant_); } void diff --git a/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp b/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp index 0ebe6efd66..39f6e33e9a 100644 --- a/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp +++ b/rclcpp/include/rclcpp/detail/subscription_callback_type_helper.hpp @@ -129,37 +129,6 @@ struct SubscriptionCallbackTypeHelper< std::function &, const rclcpp::MessageInfo &)>; }; -template -struct SubscriptionCallbackTypeHelper< - MessageT, - CallbackT, - typename std::enable_if_t< - rclcpp::function_traits::same_arguments< - CallbackT, - std::function)> - >::value - > -> -{ - using callback_type = std::function)>; -}; - -template -struct SubscriptionCallbackTypeHelper< - MessageT, - CallbackT, - typename std::enable_if_t< - rclcpp::function_traits::same_arguments< - CallbackT, - std::function, const rclcpp::MessageInfo &)> - >::value - > -> -{ - using callback_type = - std::function, const rclcpp::MessageInfo &)>; -}; - } // namespace detail } // namespace rclcpp diff --git a/rclcpp/test/rclcpp/test_any_subscription_callback.cpp b/rclcpp/test/rclcpp/test_any_subscription_callback.cpp index d83580fc7b..423b9e6b2e 100644 --- a/rclcpp/test/rclcpp/test_any_subscription_callback.cpp +++ b/rclcpp/test/rclcpp/test_any_subscription_callback.cpp @@ -20,7 +20,6 @@ #include #include -#define RCLCPP_AVOID_DEPRECATIONS_FOR_UNIT_TESTS 1 #include "rclcpp/any_subscription_callback.hpp" #include "test_msgs/msg/empty.hpp" @@ -741,78 +740,3 @@ INSTANTIATE_TEST_SUITE_P( ), format_parameter_with_ta ); - -// -// Versions of `std::shared_ptr` -// -void shared_ptr_free_func(std::shared_ptr) {} -void shared_ptr_w_info_free_func( - std::shared_ptr, const rclcpp::MessageInfo &) -{} - -INSTANTIATE_TEST_SUITE_P( - SharedPtrCallbackTests, - DispatchTests, - ::testing::Values( - // lambda - InstanceContext{"lambda", rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr) {})}, - InstanceContext{"lambda_with_info", - rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr, const rclcpp::MessageInfo &) {})}, - // free function - InstanceContext{"free_function", rclcpp::AnySubscriptionCallback().set( - shared_ptr_free_func)}, - InstanceContext{"free_function_with_info", - rclcpp::AnySubscriptionCallback().set( - shared_ptr_w_info_free_func)}, - // bind function - BindContext>("bind_method"), - BindContext, - const rclcpp::MessageInfo &>( - "bind_method_with_info") - ), - format_parameter -); - -void shared_ptr_ta_free_func(std::shared_ptr) {} -void shared_ptr_ta_w_info_free_func( - std::shared_ptr, const rclcpp::MessageInfo &) -{} - -INSTANTIATE_TEST_SUITE_P( - SharedPtrCallbackTests, - DispatchTestsWithTA, - ::testing::Values( - // lambda - InstanceContext{"lambda_ta", rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr) {})}, - InstanceContext{"lambda_ta_with_info", - rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr, const rclcpp::MessageInfo &) {})}, - InstanceContext{"lambda", rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr) {})}, - InstanceContext{"lambda_with_info", - rclcpp::AnySubscriptionCallback().set( - [](std::shared_ptr, const rclcpp::MessageInfo &) {})}, - // free function - InstanceContext{"free_function_ta", rclcpp::AnySubscriptionCallback().set( - shared_ptr_ta_free_func)}, - InstanceContext{"free_function_ta_with_info", - rclcpp::AnySubscriptionCallback().set( - shared_ptr_ta_w_info_free_func)}, - InstanceContext{"free_function", rclcpp::AnySubscriptionCallback().set( - shared_ptr_free_func)}, - InstanceContext{"free_function_with_info", - rclcpp::AnySubscriptionCallback().set( - shared_ptr_w_info_free_func)}, - // bind function - BindContext>("bind_method_ta"), - BindContext, const rclcpp::MessageInfo &>( - "bind_method_ta_with_info"), - BindContext>("bind_method"), - BindContext, const rclcpp::MessageInfo &>( - "bind_method_with_info") - ), - format_parameter_with_ta -); diff --git a/rclcpp_action/test/test_client.cpp b/rclcpp_action/test/test_client.cpp index c461d1eeed..88e0cf38f4 100644 --- a/rclcpp_action/test/test_client.cpp +++ b/rclcpp_action/test/test_client.cpp @@ -242,7 +242,7 @@ class TestClient : public ::testing::Test // to be executed. { auto subscription = client_node->create_subscription( - "test_topic", 10, [](const test_msgs::msg::Empty::SharedPtr) {}); + "test_topic", 10, [](const test_msgs::msg::Empty::ConstSharedPtr) {}); subscription_content_filter_supported_ = subscription->is_cft_supported(); } } diff --git a/rclcpp_action/test/test_generic_client.cpp b/rclcpp_action/test/test_generic_client.cpp index 0bca9acd0b..df3ccb2fa4 100644 --- a/rclcpp_action/test/test_generic_client.cpp +++ b/rclcpp_action/test/test_generic_client.cpp @@ -233,7 +233,7 @@ class TestGenericClient : public ::testing::Test // to be executed. { auto subscription = client_node->create_subscription( - "test_topic", 10, [](const test_msgs::msg::Empty::SharedPtr) {}); + "test_topic", 10, [](const test_msgs::msg::Empty::ConstSharedPtr) {}); subscription_content_filter_supported_ = subscription->is_cft_supported(); } } From 27a77ae097454113abd43c05f45594e35d1c0eab Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Fri, 27 Nov 2026 11:51:27 -0800 Subject: [PATCH 2/2] Only check first callback argument if there is one Callbacks without parameters, which are invalid anyway, made `argument_type<0>` fail in `std::tuple_element`, adding unrelated errors before the usual `no match for 'operator='` error. Signed-off-by: Christophe Bedard --- .../rclcpp/any_subscription_callback.hpp | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/rclcpp/include/rclcpp/any_subscription_callback.hpp b/rclcpp/include/rclcpp/any_subscription_callback.hpp index e7666043c8..4bd5ab8cf0 100644 --- a/rclcpp/include/rclcpp/any_subscription_callback.hpp +++ b/rclcpp/include/rclcpp/any_subscription_callback.hpp @@ -352,17 +352,20 @@ class AnySubscriptionCallback // converted to one another, e.g. shared_ptr and unique_ptr. using scbth = detail::SubscriptionCallbackTypeHelper; - using CallbackMessageT = std::decay_t< - typename rclcpp::function_traits::function_traits< - typename scbth::callback_type>::template argument_type<0>>; - constexpr bool is_shared_ptr_to_non_const_message = - std::is_same>::value || - std::is_same>::value || - std::is_same>::value; - static_assert( - !is_shared_ptr_to_non_const_message, - "subscription callbacks taking a std::shared_ptr to a non-const message are not " - "supported, use std::shared_ptr instead"); + using CallbackTraits = + rclcpp::function_traits::function_traits; + if constexpr (CallbackTraits::arity > 0) { + using CallbackMessageT = + std::decay_t>; + constexpr bool is_shared_ptr_to_non_const_message = + std::is_same>::value || + std::is_same>::value || + std::is_same>::value; + static_assert( + !is_shared_ptr_to_non_const_message, + "subscription callbacks taking a std::shared_ptr to a non-const message are not " + "supported, use std::shared_ptr instead"); + } // Use the discovered type to force the type of callback when assigning // into the variant.