自动生成的ROS(机器人操作系统)消息C ++头文件包含以下类型定义:
typedef ::std_msgs::Header_<std::allocator<void> > Header;
这里的
std::allocator<void>
是什么意思?为什么模板类型是 void
?它代表什么含义?在什么情况下使用?这里是
std::allocator<>
的文档:
- https://www.cplusplus.com/reference/memory/allocator/
- https://en.cppreference.com/w/cpp/memory/allocator
以下是需要查看的自动生成示例文件:http://docs.ros.org/en/electric/api/std_msgs/html/msg__gen_2cpp_2include_2std__msgs_2Header_8h_source.html,第一行在第116行。
这是 ROS 自动生成的消息
Header_
类的开头:template <class ContainerAllocator>
struct Header_ {
下面是从自动生成的Header.h中提取出来的更多上下文,底部有各种typedef
:
template <class ContainerAllocator>
struct Header_ {
typedef Header_<ContainerAllocator> Type;
Header_()
: seq(0)
, stamp()
, frame_id()
{
}
Header_(const ContainerAllocator& _alloc)
: seq(0)
, stamp()
, frame_id(_alloc)
{
}
typedef uint32_t _seq_type;
uint32_t seq;
typedef ros::Time _stamp_type;
ros::Time stamp;
typedef std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > _frame_id_type;
std::basic_string<char, std::char_traits<char>, typename ContainerAllocator::template rebind<char>::other > frame_id;
private:
static const char* __s_getDataType_() { return "std_msgs/Header"; }
public:
ROS_DEPRECATED static const std::string __s_getDataType() { return __s_getDataType_(); }
ROS_DEPRECATED const std::string __getDataType() const { return __s_getDataType_(); }
private:
static const char* __s_getMD5Sum_() { return "2176decaecbce78abc3b96ef049fabed"; }
public:
ROS_DEPRECATED static const std::string __s_getMD5Sum() { return __s_getMD5Sum_(); }
ROS_DEPRECATED const std::string __getMD5Sum() const { return __s_getMD5Sum_(); }
private:
static const char* __s_getMessageDefinition_() { return "# Standard metadata for higher-level stamped data types.\n\
# This is generally used to communicate timestamped data \n\
# in a particular coordinate frame.\n\
# \n\
# sequence ID: consecutively increasing ID \n\
uint32 seq\n\
#Two-integer timestamp that is expressed as:\n\
# * stamp.secs: seconds (stamp_secs) since epoch\n\
# * stamp.nsecs: nanoseconds since stamp_secs\n\
# time-handling sugar is provided by the client library\n\
time stamp\n\
#Frame this data is associated with\n\
# 0: no frame\n\
# 1: global frame\n\
string frame_id\n\
\n\
"; }
public:
ROS_DEPRECATED static const std::string __s_getMessageDefinition() { return __s_getMessageDefinition_(); }
ROS_DEPRECATED const std::string __getMessageDefinition() const { return __s_getMessageDefinition_(); }
ROS_DEPRECATED virtual uint8_t *serialize(uint8_t *write_ptr, uint32_t seq) const
{
ros::serialization::OStream stream(write_ptr, 1000000000);
ros::serialization::serialize(stream, seq);
ros::serialization::serialize(stream, stamp);
ros::serialization::serialize(stream, frame_id);
return stream.getData();
}
ROS_DEPRECATED virtual uint8_t *deserialize(uint8_t *read_ptr)
{
ros::serialization::IStream stream(read_ptr, 1000000000);
ros::serialization::deserialize(stream, seq);
ros::serialization::deserialize(stream, stamp);
ros::serialization::deserialize(stream, frame_id);
return stream.getData();
}
ROS_DEPRECATED virtual uint32_t serializationLength() const
{
uint32_t size = 0;
size += ros::serialization::serializationLength(seq);
size += ros::serialization::serializationLength(stamp);
size += ros::serialization::serializationLength(frame_id);
return size;
}
typedef boost::shared_ptr< ::std_msgs::Header_<ContainerAllocator> > Ptr;
typedef boost::shared_ptr< ::std_msgs::Header_<ContainerAllocator> const> ConstPtr;
boost::shared_ptr<std::map<std::string, std::string> > __connection_header;
}; // struct Header
typedef ::std_msgs::Header_<std::allocator<void> > Header;
typedef boost::shared_ptr< ::std_msgs::Header> HeaderPtr;
typedef boost::shared_ptr< ::std_msgs::Header const> HeaderConstPtr;
相关
我认为以下内容并不重复:
- 什么是allocator<T> - 不是重复问题,因为我询问的是当
T
是void
时的特定情况,而不是std::allocator<>
的一般情况。 - std::allocator<void>的弃用 - 不是重复问题,因为我不想知道为什么它已在C++20中被弃用或更改,我想了解
std::allocator<void>
的一般情况,它是做什么的,何时/为什么要使用它。 - https://answers.ros.org/question/212857/what-is-constptr/
T
是void
的,这有点特殊。 - Daniel Langr