std::byte

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
byte
(C++17)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(deprecated in C++17)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(deprecated in C++17)(C++17)
 
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.

Like the character types (char, unsigned char, signed 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.

Non-member functions

std::to_integer

template <class IntegerType>
 constexpr IntegerType to_integer(std::byte b) noexcept;
(since C++17)

Equivalent to: return IntegerType(b); This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.

std::operator<<=,operator>>=

template <class IntegerType>
 constexpr std::byte& operator<<=(std::byte& b, IntegerType shift) noexcept;
(1) (since C++17)
template <class IntegerType>
 constexpr std::byte& operator>>=(std::byte& b, IntegerType shift) noexcept;
(2) (since C++17)
1) Equivalent to: return b = std::byte(static_cast<unsigned char>(b) << shift); This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.
2) Equivalent to: return b = std::byte(static_cast<unsigned char>(b) >> shift);

This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.

std::operator<<,operator>>

template <class IntegerType>
 constexpr std::byte operator <<(std::byte b, IntegerType shift) noexcept;
(1) (since C++17)
template <class IntegerType>
 constexpr std::byte operator >>(std::byte b, IntegerType shift) noexcept;
(2) (since C++17)
1) Equivalent to: return std::byte(static_cast<unsigned char>(b) << shift); This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.
2) Equivalent to: return std::byte(static_cast<unsigned char>(b) >> shift);

This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.

std::operator|=,operator&=,operator^=

constexpr std::byte& operator|=(std::byte& l, std::byte r) noexcept;
(1) (since C++17)
constexpr std::byte& operator&=(std::byte& l, std::byte r) noexcept;
(2) (since C++17)
constexpr std::byte& operator^=(std::byte& l, std::byte r) noexcept;
(3) (since C++17)
1) Equivalent to: return l = std::byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));.
2) Equivalent to: return l = std::byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));.
3) Equivalent to: return l = std::byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));.

std::operator|,operator&,operator^,operator~

constexpr std::byte operator|(std::byte l, std::byte r) noexcept;
(1) (since C++17)
constexpr std::byte operator&(std::byte l, std::byte r) noexcept;
(2) (since C++17)
constexpr std::byte operator^(std::byte l, std::byte r) noexcept;
(3) (since C++17)
constexpr std::byte operator~(std::byte b) noexcept;
(4) (since C++17)
1) Equivalent to: return std::byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));.
2) Equivalent to: return std::byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));.
3) Equivalent to: return std::byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));.
4) Equivalent to: return std::byte(~static_cast<unsigned char>(b));

Notes

A numeric value n can be converted to a byte value using std::byte{n} , due to C++17 relaxed enum class initialization rules.

A byte can be converted to a numeric value (such as to produce a integer hash of an object) using std::to_integer

Example