Autogenerated ROS (Robot Operating System) message C++ header files contain typedefs like this:
typedef ::std_msgs::Header_<std::allocator<void> > Header;
What does std::allocator<void>
mean here? Why is the template type void
? What does it mean? When is it used?
Here is the documentation for std::allocator<>
:
- https://www.cplusplus.com/reference/memory/allocator/
- https://en.cppreference.com/w/cpp/memory/allocator
Here is the example autogenerated file to look at: http://docs.ros.org/en/electric/api/std_msgs/html/msg__gen_2cpp_2include_2std__msgs_2Header_8h_source.html.
That first line above is line 116.
This is the start of the autogenerated ROS message Header_
class:
template <class ContainerAllocator>
struct Header_ {
Here is a little more context from the autogenerated Header.h, with the various typedef
s at the bottom:
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;
Related
I believe these are not duplicates:
- What is allocator<T> - not a duplicate because I'm asking about the specific case of
T
isvoid
, not the general case of what is astd::allocator<>
. - Deprecation of std::allocator<void> - not a duplicate because I'm not wondering why it has been deprecated or changed in C++20, I'm asking what the
std::allocator<void>
case is in general, what it does, and when/why to use it. - https://answers.ros.org/question/212857/what-is-constptr/
c++ what is a std::allocator
into a search engine; does that help? – SundaT
beingvoid
, which is kind of special. – Katsuyamastd::allocator<>
in general for about 1 hr now, but neither that question nor the other sources I've come across mention anything about usingvoid
as the template type for it, nor how that affects it. – Roulersstd::unorder_map
takes an allocator type for itsvalue_type
. But, internally, the map needs to allocate nodes, so it needs to "rebind" this allocator type to a different value type. This, for example, allows one to use memory pooling for node allocations (one just need to be aware that the allocator is used for the array of buckets as well). – Katsuyama