c++ - 如何使用类似 `std::basic_istream<std::byte>` 的东西

标签 c++ io std c++17

本题旨在使用std::byte具有标准输入输出。

是否有任何计划为 read(_bytes) 添加适当的函数重载?和 write(_bytes)basic_istream<CharT> 的接口(interface)和 basic_ostream<CharT>在未来的标准?有什么理由反对它?我知道 CharT* - 应保留过载。我可以做什么来使用 std::byte ?我目前在我的项目功能中定义

std::istream& read(std::istream&, std::byte*, std::streamsize)
std::ostream& write(std::ostream&, const std::byte*, std::streamsize)

这些使用reinterpret_cast<>char*分别const char*但我相信这取决于 char 的大小.我错了吗?是char总是1 byte

我试着制作 std::basic_istream<std::byte>但它不见了std::char_traits<std::byte>等等。有没有人已经让这种东西起作用了?

最佳答案

不要。

无论您是在“文本模式”还是“二进制模式”下操作,您基本上仍在做的是对字符进行操作。

std::byte 不是为了这个目的,这就是它没有这些功能的原因。事实上,它是故意引入的而不是拥有它们!

enum class byte : unsigned char {} ; (since C++17)

std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.

Like char and unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type. A byte is only a collection of bits, and only bitwise logic operators are defined for it.

http://en.cppreference.com/w/cpp/types/byte


Did anyone make this kind of thing work already?

不,如上所述,每个人都故意不这样做。

使用 charunsigned char,就像我们几十年来所做的那样!

关于c++ - 如何使用类似 `std::basic_istream<std::byte>` 的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735918/

相关文章:

c++ - std::priority_queue 复杂类型?

c++ - ADTF 过滤器触发器的默认值

c++ - 用 gcc 编译 asio

java - 从 Android 编写的文本文件中读取数值

java - JPG 图像在 Java 中通过套接字发送时损坏或损坏

c++ - 调用指向已释放对象方法的 std::function 对象

C++ 映射 : add pair to the end of the map

c++ - 函数模板无法编译

c++ - 没有看到全局结构

java - 如何在客户端和服务器之间建立多个 IO 流?