SFINAE 模板错误

SFINAE Template Error

本文关键字:错误 SFINAE      更新时间:2023-10-16

我正在使用SFINEA订阅ROS中的通用主题,它侦听每个主题,并使用SFINEA返回header.stamp时间(如果存在(。这在反序列化中工作得更快。唯一的问题是我在设置订阅者时遇到问题。我不断收到以下编译错误:

CMakeFiles/performance_tracker.dir/src/performance_tracker.cpp.o: 
In function   PerformanceTracker::topicCallback(boost::shared_ptr<topic_tools::ShapeShifter>)
ros/src/performance_tracker/src/performance_tracker.cpp:32:
undefined reference to boost::disable_if<timewarp::has_header<boost::shared_ptr<topic_tools::ShapeShifter> >, ros::Time>::type
timewarp::extractTime<boost::shared_ptr<topic_tools::ShapeShifter> >(boost::shared_ptr<topic_tools::ShapeShifter>)

主要

 // Subscribe To Generic Message 
 _sub =  _nh.subscribe( _topicName, 1, &PerformanceTracker::topicCallback, this);

void PerformanceTracker::topicCallback(const boost::shared_ptr<topic_tools::ShapeShifter> data){
//Current Time
ros::Time begin = ros::Time::now();
ros::Time timePublished = timewarp::extractTime<boost::shared_ptr<topic_tools::ShapeShifter>>(data);
}

命名空间类

namespace timewarp
{
template <typename T>
struct has_header {
    typedef char yes[1];
    typedef char no[2];
    template <typename C>
    static yes& test(typename C::_header_type*);
    template <typename>
    static no& test(...);
    // If the "sizeof" the result of calling test<T>(0) would be equal to the sizeof(yes),
    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
template<class MsgType>
typename boost::enable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<topic_tools::ShapeShifter> data)
{
    boost::shared_ptr<MsgType> ptr = data->instantiate<MsgType>();
    assert(ptr);
    return ptr->header.stamp;
}
template<class MsgType>
typename boost::disable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<topic_tools::ShapeShifter>);
}

您正在将boost::shared_ptr<topic_tools::ShapeShifter>类型作为MsgType传递给has_header<T>而不是topic_tools::ShapeShifter

我相信你想做:

ros::Time timePublished = timewarp::extractTime<topic_tools::ShapeShifter>(data);

并在您使用时使用 const refs 参数:)

template<class MsgType>
typename boost::enable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<MsgType>& data)
{
    boost::shared_ptr<MsgType> ptr = data->instantiate<MsgType>();
    assert(ptr);
    return ptr->header.stamp;
}
template<class MsgType>
typename boost::disable_if<has_header<MsgType>, ros::Time>::type extractTime(const boost::shared_ptr<MsgType>&)
{
    return ros::Time::now(); 
    // or whatever ros time you need to return for types without headers
}
}