如何使用类似 std::basic_istream<std::byte> 的东西
How to use something like `std::basic_istream<std::byte>`
本题旨在使用std::byte
标准输入输出。
是否有任何计划在未来的标准中为 read(_bytes)
和 write(_bytes)
添加适当的函数重载到 basic_istream<CharT>
和 basic_ostream<CharT>
的接口?有什么理由反对它?我知道 CharT*
-overloads 应该保留。我可以做什么来使用 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>
等等。有没有人让这种东西起作用了?
不要。
无论您是在 "text mode" 还是 "binary mode" 中操作,您基本上仍在做的是对 个字符。
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.
Did anyone make this kind of thing work already?
不,如上所述,每个人都故意不这样做。
使用 char
或 unsigned char
,就像我们几十年来所做的那样!
P2146: Modern std::byte stream IO for C++ is a proposal related to your request. The status is tracked on Github.
本题旨在使用std::byte
标准输入输出。
是否有任何计划在未来的标准中为 read(_bytes)
和 write(_bytes)
添加适当的函数重载到 basic_istream<CharT>
和 basic_ostream<CharT>
的接口?有什么理由反对它?我知道 CharT*
-overloads 应该保留。我可以做什么来使用 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>
等等。有没有人让这种东西起作用了?
不要。
无论您是在 "text mode" 还是 "binary mode" 中操作,您基本上仍在做的是对 个字符。
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
,就像我们几十年来所做的那样!
P2146: Modern std::byte stream IO for C++ is a proposal related to your request. The status is tracked on Github.