Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of std::byte?

Tags:

c++

c++17

byte

People also ask

How do you define a byte in C++?

@Ben: The C and C++ standards unambiguously define a "byte" as the size of a char , which is at least 8 bits. The term "byte" may be defined differently in other contexts, but when discussing C or C++ it's better to stick to the standard's definition.

What is unsigned char in C++?

unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char). So it means that the range of unsigned char data type ranges from 0 to 255. Syntax: unsigned char [variable_name] = [value]


You are perhaps misunderstanding things.

byte is very much intended for "accessing memory". You are intended to use the type when the storage is just a sequence of bytes rather than an array of characters.

Iostream types cannot be specialized with byte, since they're designed around characters as their interface. That is, they do not think of files as sequences of bytes; they think of them as sequences of characters. Now, you can certainly read directly into a byte array by using a cast or two. But that's not the way iostream natively thinks.

You have to make a distinction between the way iostream works and the way files work. Iostream is just one file IO library, after all; it is hardly the end-all, be-all of file APIs.

Most file APIs for reading binary data take void* rather than character arrays. std::fread/fwrite, and so forth.

That is, you should think of this, not as a problem with std::byte, but as a problem with iostream. Just another in a long line of them.