rclcpp  master
C++ ROS Client Library API
Public Types | List of all members
rclcpp::detail::SubscriptionCallbackTypeHelper< MessageT, CallbackT, Enable > Struct Template Reference

Template metaprogramming helper used to resolve the callback argument into a std::function. More...

#include <subscription_callback_type_helper.hpp>

Public Types

using callback_type = typename rclcpp::function_traits::as_std_function< CallbackT >::type
 

Detailed Description

template<typename MessageT, typename CallbackT, typename Enable = void>
struct rclcpp::detail::SubscriptionCallbackTypeHelper< MessageT, CallbackT, Enable >

Template metaprogramming helper used to resolve the callback argument into a std::function.

Sometimes the CallbackT is a std::function already, but it could also be a function pointer, lambda, bind, or some variant of those. In some cases, like a lambda where the arguments can be converted between one another, e.g. std::function<void (shared_ptr<...>)> and std::function<void (unique_ptr<...>)>, you need to make that not ambiguous by checking the arguments independently using function traits rather than rely on overloading the two std::function types.

This issue, with the lambda's, can be demonstrated with this minimal program:

#include <functional> #include <memory>

void f(std::function<void (std::shared_ptr<int>)>) {} void f(std::function<void (std::unique_ptr<int>)>) {}

int main() { // Fails to compile with an "ambiguous call" error. f([](std::shared_ptr<int>){});

// Works. std::function<void (std::shared_ptr<int>)> cb = [](std::shared_ptr<int>){}; f(cb); }

If this program ever starts working in a future version of C++, this class may become redundant.

This helper works by using SFINAE with rclcpp::function_traits::same_arguments<> to narrow down the exact std::function<> type for the given CallbackT.

Member Typedef Documentation

◆ callback_type

template<typename MessageT , typename CallbackT , typename Enable = void>
using rclcpp::detail::SubscriptionCallbackTypeHelper< MessageT, CallbackT, Enable >::callback_type = typename rclcpp::function_traits::as_std_function<CallbackT>::type

The documentation for this struct was generated from the following file: