问题描述
这个问题旨在将 std::byte
与标准输入输出一起使用.
是否有计划将read(_bytes)
和write(_bytes)
的适当函数重载添加到basic_istream
的接口中> 和 basic_ostream
在未来的标准中?什么理由反对它?我知道应该保留 CharT*
重载.如何使用 std::byte
?我目前在我的项目功能中定义
std::istream&读取(std::istream&,std::byte*,std::streamsize)std::ostream&写(std::ostream&,const std::byte*,std::streamsize)
这些使用 reinterpret_cast<>
到 char*
resp.const char*
但我相信这取决于 char
的大小.我错了吗?char
总是 1 字节
吗?
我试图制作 std::basic_istream
但它缺少 std::char_traits
等等.有没有人已经做过这种事情?
不要.
无论您是在文本模式"还是二进制模式"下操作,您所做的基本上都是对字符进行操作.
std::byte
不是为了这个目的,这就是它没有这些功能的原因.确实,它是故意引入而不是来拥有它们的!
enum class byte : unsigned char {} ;
(C++17 起)
std::byte
是一种独特的类型,它实现了 C++ 语言定义中指定的字节概念.
与char
和unsigned char
一样,它可以用来访问被其他对象占用的原始内存(对象表示),但与那些类型不同的是,它不是字符类型,也不是算术类型.一个字节只是位的集合,只为它定义了按位逻辑运算符.
http://en.cppreference.com/w/cpp/types/byte
<小时><块引用>
有人已经让这种事情奏效了吗?
不,正如上文所述,每个人都故意不这样做.
使用 char
或 unsigned char
,就像我们几十年来所做的那样!
This question aims for using std::byte
with standard input-output.
Are there any plans to add proper function overloads for read(_bytes)
and write(_bytes)
to the interfaces of basic_istream<CharT>
and basic_ostream<CharT>
in a future standard? What reasons speak against it? I understand that the CharT*
-overloads should be kept. What can I do to use std::byte
? I currently define in my project functions
std::istream& read(std::istream&, std::byte*, std::streamsize)
std::ostream& write(std::ostream&, const std::byte*, std::streamsize)
These use reinterpret_cast<>
to char*
resp. const char*
but I believe this depends on the size of char
. Am I wrong? Is char
always 1 byte
?
I tried to make std::basic_istream<std::byte>
but it is missing std::char_traits<std::byte>
and so on. Did anyone make this kind of thing work already?
Don't.
Whether you're operating in "text mode" or "binary mode", what you are still doing fundamentally is acting on characters.
std::byte
is not for this purpose, and that's why it does not have these features. Indeed, it was deliberately introduced not to have them!
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.http://en.cppreference.com/w/cpp/types/byte
Did anyone make this kind of thing work already?
No, everyone deliberately didn't, as explored above.
Use char
or unsigned char
, as we have done for decades!
这篇关于如何使用类似`std::basic_istream<std::byte>`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!