std::byte on odd platforms

前端 未结 1 1463
南笙
南笙 2021-01-18 12:18

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 c

相关标签:
1条回答
  • 2021-01-18 13:00

    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.

    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.

    • 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

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

    0 讨论(0)
提交回复
热议问题