什么是 void `std::allocator`?即:`std::allocator<void>`

Gab*_*les 8 c++ allocator ros

相关但不重复:请参阅此答案的底部,在单击此问题下方的“关闭”按钮之前,我会解决您可能想要声明的重复项。

自动生成的ROS(机器人操作系统)消息 C++ 头文件包含如下类型定义:

typedef  ::std_msgs::Header_<std::allocator<void> > Header;
Run Code Online (Sandbox Code Playgroud)

std::allocator<void>这里是什么意思?为什么是模板类型void?这是什么意思?什么时候使用?

这是文档std::allocator<>

  1. https://www.cplusplus.com/reference/memory/allocator/
  2. 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_ {
Run Code Online (Sandbox Code Playgroud)

这是自动生成的Header.h 的更多上下文,typedef底部有各种s:

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;
Run Code Online (Sandbox Code Playgroud)

有关的:

  1. [不是重复]什么是 allocator<T> - 不是重复,因为我问的T是 is的具体情况,而void不是 a 的一般情况std::allocator<>
  2. [不是重复]弃用 std::allocator<void> - 不是重复,因为我不知道为什么它在 C++20 中被弃用或更改,我问的std::allocator<void>是一般情况是什么,它是什么会,以及何时/为什么使用它。
  3. https://answers.ros.org/question/212857/what-is-constptr/

Leo*_*lev 5

std::allocator<void>是一种分配器类型,专门用于通过rebind模板为特定对象声明其他分配器类型。

在您的情况下,Headertypedef 基本上只是说默认分配器Header_std::allocator. Header_使用它std::allocator<char>frame_id. 我想在风格方面它也可能排std::allocator<char>在第一位(在 typedef 处),因为Header_在这一点上,它std::string仅用于但Header_看起来不像普通容器的char类似std::stringstd::vector如此明确的泛型std::allocator<void>更有意义。在这种情况下更重要的是在自动生成代码的脚本或模板中使用这种分配器更容易。

有关更多信息,请检查: