Is there 'byte' data type in C++?

后端 未结 8 492
耶瑟儿~
耶瑟儿~ 2020-12-04 14:33

If exists is there header file to include?

This code give compilation error:

#include 

using namespace std;

int main()
{
    byte b         


        
相关标签:
8条回答
  • 2020-12-04 14:42

    No, there is no type called "byte" in C++. What you want instead is unsigned char (or, if you need exactly 8 bits, uint8_t from <cstdint>, since C++11). Note that char is not necessarily an accurate alternative, as it means signed char on some compilers and unsigned char on others.

    0 讨论(0)
  • 2020-12-04 14:45

    Using C++11 there is a nice version for a manually defined byte type:

    enum class byte : std::uint8_t {};
    

    That's at least what the GSL does.

    Starting with C++17 (almost) this very version is defined in the standard as std::byte (thanks Neil Macintosh for both).

    0 讨论(0)
  • 2020-12-04 14:46

    There's also byte_lite, compatible with C++98, C++11 and later.

    0 讨论(0)
  • 2020-12-04 14:49

    No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte:

    typedef bitset<8> BYTE;
    

    NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows.

    Edit: In response to the suggestion that the answer is wrong. The answer is not wrong. The question was "Is there a 'byte' data type in C++?". The answer was and is: "No there is no byte data type in C++" as answered.

    With regards to the suggested possible alternative for which it was asked why is the suggested alternative better?

    According to my copy of the C++ standard, at the time:

    "Objects declared as characters (char) shall be large enough to store any member of the implementations basic character set": 3.9.1.1

    I read that to suggest that if a compiler implementation requires 16 bits to store a member of the basic character set then the size of a char would be 16 bits. That today's compilers tend to use 8 bits for a char is one thing, but as far as I can tell there is certainly no guarantee that it will be 8 bits.

    On the other hand, "the class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N." : 20.5.1. In otherwords by specifying 8 as the template parameter I end up with an object that can store a sequence consisting of 8 bits.

    Whether or not the alternative is better to char, in the context of the program being written, therefore depends, as far as I understand, although I may be wrong, upon your compiler and your requirements at the time. It was therefore upto the individual writing the code, as far as I'm concerned, to do determine whether the suggested alternative was appropriate for their requirements/wants/needs.

    0 讨论(0)
  • 2020-12-04 14:59
    namespace std
    {
      // define std::byte
      enum class byte : unsigned char {};
    
    };
    

    This if your C++ version does not have std::byte will define a byte type in namespace std. Normally you don't want to add things to std, but in this case it is a standard thing that is missing.

    std::byte from the STL does much more operations.

    0 讨论(0)
  • 2020-12-04 15:01

    Yes, there is std::byte (defined in <cstddef>).

    C++ 17 introduced it.

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