Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a void `std::allocator`? ie: `std::allocator<void>`

Tags:

c++

allocator

ros

Related, but not duplicates: please see bottom of this answer where I address the duplicates which you may want to claim this is, before you click the "close" button below this question.

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<>:

  1. https://www.cplusplus.com/reference/memory/allocator/
  2. 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 typedefs 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:

  1. [Not a duplicate] What is allocator<T> - not a duplicate because I'm asking about the specific case of T is void, not the general case of what is a std::allocator<>.
  2. [Not a duplicate] 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.
  3. https://answers.ros.org/question/212857/what-is-constptr/
like image 517
Gabriel Staples Avatar asked Apr 12 '21 06:04

Gabriel Staples


1 Answers

std::allocator<void> is an allocator type that is used exclusively to declare other allocator types for specific objects via rebind template.

In your case Header typedef basically just says that default allocator for Header_ is std::allocator. Header_ uses it to create std::allocator<char> for frame_id. I guess style-wise it might as well be std::allocator<char> in first place (at the typedef) because Header_ at this point uses it for std::string only but Header_ doesn't look like plain container of char like std::string or std::vector so explicit sort of generic std::allocator<void> makes more sense. And probability what is more important in this case is that it is easier to use such allocator in script or template that auto-generates code.

For more information check:

  • 15.3.1 Using the Standard Allocator Interface notes on rebind template
  • Why is allocator<void> deprecated? post by Andrey Semashev in discussion on Google Groups
  • AllocatorAwareContainer requirement on cppreference.com
like image 94
Leonid Vasilev Avatar answered Sep 23 '22 11:09

Leonid Vasilev