如何使用像`std :: basic_istream< std :: byte>`这样的东西 [英] How to use something like `std::basic_istream<std::byte>`

查看:305
本文介绍了如何使用像`std :: basic_istream< std :: byte>`这样的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的目的是使用标准输入输出 std :: byte

This question aims for using std::byte with standard input-output.

有没有计划为读取(_bytes)写入(_bytes)添加适当的函数重载到 basic_istream< CharT> 和 basic_ostream< CharT> 在未来的标准中?有什么理由反对呢?我知道应该保留 CharT * -overloads。我该怎么做才能使用 std :: byte ?我目前在我的项目函数中定义

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)

这些使用 reinterpret_cast<> char * resp。 const char * 但我相信这取决于 char 的大小。我错了吗?是 char 总是 1字节

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?

我试过make std :: basic_istream< std :: byte> 但它缺少 std :: char_traits< std :: 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?

推荐答案

不要。

无论您是以文本模式还是二进制模式运行,您仍然在做什么从根本上采取字符

Whether you're operating in "text mode" or "binary mode", what you are still doing fundamentally is acting on characters.

std :: byte 不是出于此目的,这就是它没有这些功能的原因。实际上,它是故意引入而不是来拥有它们!

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!


枚举类字节: unsigned char {}; (自C ++ 17起)

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

std :: byte 是一个独特的类型,它实现了C ++语言定义中指定的byte概念。

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

char unsigned char ,它可用于访问其他对象占用的原始内存( object representation ),但与那些类型不同,它不是字符类型,也不是算术类型。一个字节只是一个位集合,只为它定义了按位逻辑运算符。

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?

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

No, everyone deliberately didn't, as explored above.

使用 char unsigned char ,正如我们几十年来所做的那样!

Use char or unsigned char, as we have done for decades!

这篇关于如何使用像`std :: basic_istream&lt; std :: byte&gt;`这样的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆