std::allocator<void>
时间: 2023-12-25 22:13:15 浏览: 21
`std::allocator<void>` 是 C++ 标准库中的一种分配器(allocator),用于在不知道要分配的对象类型的情况下进行内存分配。它不是一个可分配任何类型的分配器,而是专门为 `void*` 类型分配内存的分配器。它不支持调用构造函数或析构函数,因为它只是分配内存,而不初始化或销毁任何对象。它通常用于作为其他类型分配器的默认分配器,以便它们可以处理不同类型的对象。
相关问题
解释 typedef ::livox_ros_driver::CustomMsg_<std::allocator<void> > CustomMsg;
这行代码定义了一个别名 CustomMsg,它是由模板类 ::livox_ros_driver::CustomMsg_ 实例化得到的,其中模板实参是 std::allocator<void>。这个模板类是在命名空间 ::livox_ros_driver 中定义的。
其中 std::allocator<void> 表示使用标准库中的分配器类 std::allocator 来分配内存,但是因为模板类 ::livox_ros_driver::CustomMsg_ 并没有使用这个分配器,所以实际上这个模板实参是没有用的,只是为了满足模板类的要求而已。
最终的别名 CustomMsg 就是一个类型,它实际上就是 ::livox_ros_driver::CustomMsg_<std::allocator<void> > 的一个别名,可以用来定义变量、参数、返回值等。
没有与这些操作数匹配的 "=" 运算符C/C++(349) listener_str.cpp(12, 23): 操作数类型为: std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> = std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>
这个错误通常是因为尝试将一个类型为`std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>`的变量赋值给一个类型为`std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>`的变量,这两个类型虽然都是指向`rclcpp::Subscription`的`shared_ptr`,但是模板参数不同,无法直接赋值。
解决方法是将两个类型匹配,可以通过使用`std::static_pointer_cast`将其中一个类型转换成另一个类型,例如:
```
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> sub;
std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>> sub_const;
// 将 sub_const 转换成 sub 的类型
sub = std::static_pointer_cast<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>(sub_const);
```