本题旨在使用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
andunsigned 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.
Did anyone make this kind of thing work already?
不,如上所述,每个人都故意不这样做。
使用 char
或 unsigned char
,就像我们几十年来所做的那样!
关于c++ - 如何使用类似 `std::basic_istream<std::byte>` 的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43735918/