gpt4 book ai didi

c++ - 什么是空 `std::allocator` ?即 : `std::allocator`

转载 作者:行者123 更新时间:2023-12-03 17:15:23 29 4
gpt4 key购买 nike

相关但不重复:请参阅此答案的底部,在单击此问题下方的“关闭”按钮之前,我解决了您可能想要声明的重复项。
自动生成 ROS (Robot Operating System) message 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_ 的开始类(class):
    template <class ContainerAllocator>
    struct Header_ {
    这是自动生成的 的更多上下文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;
    有关的:
  • [ 不是重复的 ] What is allocator<T> - 不是重复的,因为我在询问 T 的具体情况是 void ,不是什么是 std::allocator<> 的一般情况.
  • [ 不是重复的 ] Deprecation of std::allocator<void> - 不是重复的,因为我不知道为什么它在 C++20 中被弃用或更改,我问的是 std::allocator<void> case 一般是,它的作用是什么,以及何时/为什么使用它。
  • https://answers.ros.org/question/212857/what-is-constptr/
  • 最佳答案

    std::allocator<void>是一种分配器类型,专门用于通过 rebind 为特定对象声明其他分配器类型。模板。
    在你的情况下 Header typedef 基本上只是说 Header_ 的默认分配器是 std::allocator . Header_用它来创建 std::allocator<char>frame_id .我想在风格方面它也可能是 std::allocator<char>首先(在typedef)因为Header_此时将其用于 std::string只是Header_看起来不像 char 的普通容器喜欢 std::stringstd::vector如此明确的泛型 std::allocator<void>更有意义。在这种情况下更重要的是在脚本或 template 中更容易使用这样的分配器。自动生成代码。
    有关更多信息,请检查:

  • 15.3.1 Using the Standard Allocator Interface关于 rebind 的注释模板
  • Why is allocator<void> deprecated? Andrey Semashev 在 Google 网上论坛讨论中发表的帖子
  • AllocatorAwareContainer cppreference.com 上的要求
  • 关于c++ - 什么是空 `std::allocator` ?即 : `std::allocator<void>` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67053471/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com