什么是void`std :: allocator`?即:`std :: allocator< void>` [英] What is a void `std::allocator`? ie: `std::allocator<void>`
问题描述
相关但不可重复:请点击此答案的底部,在单击关闭"(Close)之前,我将在此处回答您可能要声明的重复内容.该问题下方的按钮.
自动生成的 ROS(机器人操作系统)消息C ++头文件包含这样的typedef:
Autogenerated ROS (Robot Operating System) message C++ header files contain typedefs like this:
typedef ::std_msgs::Header_<std::allocator<void> > Header;
std :: allocator< void>
在这里是什么意思?为什么模板类型为 void
?这是什么意思?什么时候使用?
What does std::allocator<void>
mean here? Why is the template type void
? What does it mean? When is it used?
这是 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行.
That first line above is line 116.
这是自动生成的ROS消息 Header _
类的开始:
This is the start of the autogenerated ROS message Header_
class:
template <class ContainerAllocator>
struct Header_ {
自动生成的 Header.h 中还有一些上下文,底部带有各种 typedef
s
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;
相关:
- [非重复] 什么是分配器< T> -不是重复的,因为我要问的是
T
的具体情况是void
,而不是std :: allocator<>
. - [非重复] 弃用std :: allocator< void> -不是重复的,因为我不知道为什么在C ++ 20中不推荐使用或更改它,所以我问的一般情况是
std :: allocator< void>
情况是什么,它的作用以及何时/为什么使用它. - https://answers.ros.org/question/212857/what-is-constptr/
- [Not a duplicate] 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<>
. - [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. - https://answers.ros.org/question/212857/what-is-constptr/
推荐答案
std :: allocator< void>
是一种分配器类型,专用于通过重新绑定
模板.
std::allocator<void>
is an allocator type that is used exclusively to declare other allocator types for specific objects via rebind
template.
在您的情况下, Header
typedef基本上只是说 Header _
的默认分配器是 std :: allocator
. Header _
使用它为 frame_id
创建 std :: allocator< char>
.我认为从风格上讲,它最好也应该是 std :: allocator< char>
(在typedef处),因为此时 Header _
会将其用于仅std :: string
,但 Header _
看起来不像 std :: string
或std :: vector
,所以对通用的 std :: allocator< void>
进行显式排序更有意义.在这种情况下,更为重要的是,在脚本或
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使用标准分配器接口关于
重新绑定
模板 - 为什么分配器< void>已弃用吗?由Andrey Semashev发表在Google网上论坛上的讨论 对cppreference.com的
- AllocatorAwareContainer 要求
- 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
这篇关于什么是void`std :: allocator`?即:`std :: allocator< void>`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!