std :: byte在奇数平台上

编程入门 行业动态 更新时间:2024-10-26 13:34:54
本文介绍了std :: byte在奇数平台上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在阅读Herb Sutter关于最新C ++标准会议的博客文章时,它注意到 std :: byte 已添加到C ++ 17。初读时,我有些担心,因为它使用 unsigned char 来避免严格的别名规则带来的麻烦。

Reading Herb Sutter's blog post about the most recent C++ standard meeting, it noticed that std::byte was added to C++17. As an initial reading, I have some concerns since it uses unsigned char so that it can avoid complications with strict aliasing rules.

我最大的担心是,它如何在 CHAR_BIT 不为8的平台上工作?我曾在 CHAR_BIT 是16或32(通常是DSP)的平台上工作。鉴于 std :: byte 用于处理面向字节的内存访问,并且大多数人都理解字节来表示一个八位位组(而不是基础字符类型的大小) ),对于希望此地址可解决连续的8位内存块的个人来说,这将如何工作?

My biggest concern is, how does it work on platforms where CHAR_BIT is not 8? I have worked on/with platforms where CHAR_BIT is 16 or 32 (generally DSPs). Given that std::byte is for dealing with "byte-oriented access to memory", and most people understand byte to indicate an octet (not the size of the underlying character type), how will this work for individuals who expect that this will address contiguous 8-bit chunks of memory?

我已经看到有人认为 CHAR_BIT 是8(不知道有 CHAR_BIT 存在...)。名为 std :: byte 的类型可能会给个人带来更大的困惑。

I already see people who just assume that CHAR_BIT is 8 (not evening knowing that CHAR_BIT exists...). A type called std::byte is likely to introduce even more confusion to individuals.

我想我所期望的是他们正在引入一种类型,以允许在所有情况下都可以一致地寻址/访问顺序八位位组。在许多面向八位字节的协议中,无论什么 CHAR_BIT ,都有一个可以保证在所有平台上一次访问一个八位字节的库或类型很有用。等于给定平台上的值。

I guess that what I expected was that they were introducing a type to permit consistent addressing/access to sequential octets for all cases. There are many octet-oriented protocols where it would be useful to have a library or type that is guaranteed to access memory one octet at a time on all platforms, no matter what CHAR_BIT is equal to on the given platform.

我绝对可以理解,想要很好地指定某个东西被用作字节序列而不是字符序列,

I can definitely understand wanting to have it well specified that something is being used as a sequence of bytes rather than a sequence of characters, but it doesn't seem like being as useful as many other things might be.

推荐答案

给定 std :: byte 用于处理面向字节的内存访问,并且大多数人理解字节表示一个八位位组(而不是基础字符类型的大小) ,对于希望这将解决连续的8位内存块的个人来说,这将如何工作?

Given that std::byte is for dealing with "byte-oriented access to memory", and most people understand byte to indicate an octet (not the size of the underlying character type), how will this work for individuals who expect that this will address contiguous 8-bit chunks of memory?

您无法理解某些内容错误,然后期望世界重新调整自身以符合您的期望。

You can't understand something wrong and then expect the world to rearrange itself to fit your expectations.

大多数人认为一个字节和一个八位字节是同一件事的原因是,在大多数情况下,它是真实的。您的典型计算机中的绝大多数具有 CHAR_BIT == 8 。这并不意味着它一直都是真的。

The reason why most people think a byte and an octet are the same thing is because in most cases it is true. The vast majority of your typical computer has CHAR_BIT == 8. That doesn't mean it is true all the time.

  • 字节不是八位字节。
  • char ,签名的字符和未签名的字符的大小为一个 byte 。
  • A byte is not an octet.
  • char, signed char and unsigned char have a size of one byte.

好的一面是,人们不知道的人实际上就是不需要知道的人。如果您在一个字节组成的字节多的机器上工作,那么您是那种需要比其他任何字节都了解更多的开发人员。

The good side though is that, people who don't know that, are actually people who don't need to know. If you're working on a machine where a byte is made of more than an octet you are the kind of developer who needs to know that more than any other one.

如果我们在这里谈论理论,那么答案很简单:只需了解一个字节与一个八位位组不同。如果我们在谈论具体的东西,那么答案是您要么已经知道差异,要么就不需要知道(希望:))。最糟糕的情况是您痛苦地学习了这一点,但这是在没有异国知识的情况下在异国平台上工作的第三少部分开发人员。

If we're talking theory here, then the answer is simple: just learn that a byte is different than an octet. If we're talking concrete stuff, then the answer is that you either know the difference already or you won't need to know it (hopefully :)). The worst case is you learning this painfully, but that's the third minority group of developers working on exotic platforms without exotic knowledge.

如果您想要等效的八位字节,则它已经存在:

If you want an equivalent for octets, it already exists:

  • int8_t
  • uint8_t
  • int8_t
  • uint8_t

请注意,它们仅在直接实现时提供支持类型。

Note that they are "provided only if the implementation directly supports the type".

更多推荐

std :: byte在奇数平台上

本文发布于:2023-11-16 10:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇数   平台上   std   byte

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!