Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use cases of std::byte [duplicate]

Tags:

c++

c++17

The recent addition of std::byte to C++17 got me wondering why this type was even added to the standard at all. Even after reading the cppreference reference it's use cases don't seem clear to me.

The only use case I can come up with is that it more clearly expresses intent, as std::byte should only be treated as a collection of bits instead of a character type such as char which we used for both purposes before. Meaning that:

this:

std::vector<std::byte> memory;

Is more clear than this:

std::vector<char> memory;

Is this the only use case and reason it was added to the standard or am I missing a big point here?

like image 523
Hatted Rooster Avatar asked Jan 20 '18 10:01

Hatted Rooster


People also ask

Why use std:: byte?

The new std::byte data type does not convey character or arithmetic semantics, it is just a collection of bits. As such, it's ideal to represent raw memory. An std::byte only supports initialization from an integral type, and can be converted back to an integral type using std::to_integer().

Is memcpy in std?

std::memcpy is meant to be the fastest library routine for memory-to-memory copy. It is usually more efficient than std::strcpy, which must scan the data it copies or std::memmove, which must take precautions to handle overlapping inputs.

Where is std :: byte defined?

Defined in header <cstddef> enum class byte : unsigned char {} ; (since C++17) std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.

How do you initialize a std byte?

std::byte is an enum class that was first introduced in C++17 and can be initialized using {} with any value from 0 to 255.


1 Answers

The only use case I can come up with is that it more clearly expresses intent

I think it was one of the reasons. This paper explains the motivation behind std::byte and compares its usage with the usage of char:

Motivation and Scope

Many programs require byte-oriented access to memory. Today, such programs must use either the char, signed char, or unsigned char types for this purpose. However, these types perform a “triple duty”. Not only are they used for byte addressing, but also as arithmetic types, and as character types. This multiplicity of roles opens the door for programmer error – such as accidentally performing arithmetic on memory that should be treated as a byte value – and confusion for both programmers and tools. Having a distinct byte type improves type-safety, by distinguishing byte-oriented access to memory from accessing memory as a character or integral value. It improves readability.

Having the type would also make the intent of code clearer to readers (as well as tooling for understanding and transforming programs). It increases type-safety by removing ambiguities in expression of programmer’s intent, thereby increasing the accuracy of analysis tools.

Another reason is that std::byte is restricted in terms of operations which can be performed on this type:

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.

which ensures an additional type safety as it is mentioned in the paper above.

like image 144
Edgar Rokjān Avatar answered Sep 25 '22 14:09

Edgar Rokjān