我正在使用从网络接收的报价消息作为 uint8_t
数组的库,我可以使用 std::string
作为这些缓冲区的容器吗?
uint8_t *buffer = ...;
size_t length = ...;
std::string msg = std::string(buffer, length);
这个构造函数导致编译错误?如果我使用 reinterpret_cast
将缓冲区转换为 char*
,它会编译,但它是否定义明确且合法?
最佳答案
您可以安全地转换为 char*
,因为标准规定 char*
可以作为任何其他类型的别名。
// this would be fine
std::string msg(reinterpret_cast<char*>(buffer), length);
关于c++ - 使用字符串作为缓冲容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324498/