std::byte(3)
πΈ NAME πΈ
std::byte - std::byte
okay bestie, this is std::byte(3) from the C++ Standard Libary manual β yes, thatβs the manual name exactly, weβre not touching it π
. This page is all about the byte type that is not out here acting like a number, and itβs lowkey iconic for raw-memory vibes. β¨
β¨ Synopsis β¨
First, the shape of it:
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. Periodt.
Like unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike unsigned char, it is not a character type and is not an arithmetic type. std::byte models a mere collection of bits, supporting only bitshift operations with an integer, and bitwise and comparison operations with another std::byte. No cap, itβs literally just bits doing bit things. π¦
π«Ά Non-member functions π«Ά
Hereβs the friend group of free functions. Theyβre all (since C++17), bestie.
std::to_integer
template <class IntegerType>
constexpr IntegerType to_integer( std::byte b ) noexcept;
(since C++17)
- Equivalent to:
return IntegerType(b);This overload participates in overload resolution only ifstd::is_integral_v<IntegerType>is true.
std::operator<<=,operator>>=
template <class IntegerType>
constexpr std::byte& operator<<=( std::byte& b, IntegerType shift ) noexcept;
template <class IntegerType>
constexpr std::byte& operator>>=( std::byte& b, IntegerType shift ) noexcept;
(1) and (2) are both (since C++17).
1)Equivalent to:return b = b << shift;This overload participates in overload resolution only ifstd::is_integral_v<IntegerType>is true.2)Equivalent to:return b = b >> shift;- This overload participates in overload resolution only if
std::is_integral_v<IntegerType>is true.
- This overload participates in overload resolution only if
std::operator<<,operator>>
template <class IntegerType>
constexpr std::byte operator<<( std::byte b, IntegerType shift ) noexcept;
template <class IntegerType>
constexpr std::byte operator>>( std::byte b, IntegerType shift ) noexcept;
(1) and (2) are both (since C++17).
1)Equivalent to:return std::byte(static_cast<unsigned int>(b) << shift);This overload participates in overload resolution only ifstd::is_integral_v<IntegerType>is true.2)Equivalent to:return std::byte(static_cast<unsigned int>(b) >> shift);- This overload participates in overload resolution only if
std::is_integral_v<IntegerType>is true.
- This overload participates in overload resolution only if
std::operator|=,operator&=,operator^=
constexpr std::byte& operator|=( std::byte& l, std::byte r ) noexcept;
constexpr std::byte& operator&=( std::byte& l, std::byte r ) noexcept;
constexpr std::byte& operator^=( std::byte& l, std::byte r ) noexcept;
(1), (2), and (3) are all (since C++17).
1)Equivalent to:return l = l | r;.2)Equivalent to:return l = l & r;.3)Equivalent to:return l = l ^ r;.
std::operator|,operator&,operator^,operator~
constexpr std::byte operator|( std::byte l, std::byte r ) noexcept;
constexpr std::byte operator&( std::byte l, std::byte r ) noexcept;
constexpr std::byte operator^( std::byte l, std::byte r ) noexcept;
constexpr std::byte operator~( std::byte b ) noexcept;
(1), (2), (3), and (4) are all (since C++17).
1)Equivalent to:return std::byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));.2)Equivalent to:return std::byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));.3)Equivalent to:return std::byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));.4)Equivalent to:return std::byte(~static_cast<unsigned int>(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. Cute, right? π
A byte can be converted to a numeric value (such as to produce an integer hash of an object) the usual way with an explicit conversion or alternatively with std::to_integer. Obsessed with the flexibility, honestly.
Feature-test macro vibes:
Feature-test macro Value Std Feature
__cpp_lib_byte 201603L (C++17) std::byte
π» Example π»
// Run this code
#include <bitset>
#include <cstddef>
#include <iostream>
#include <utility>
std::ostream& operator<<(std::ostream& os, std::byte b)
{
return os << std::bitset<8>(std::to_integer<int>(b));
}
int main()
{
// std::byte y = 1; // Error: cannot convert int to byte.
std::byte y{1}; // OK
// if (y == 13) {} // Error: cannot be compared.
if (y == std::byte{13}) {} // OK, bytes are comparable
int arr[]{1, 2, 3};
// int c = a[y]; // Error: array subscript is not an integer
[[maybe_unused]] int i = arr[std::to_integer<int>(y)]; // OK
[[maybe_unused]] int j = arr[std::to_underlying(y)]; // OK
std::byte b{42};
std::cout << "1. " << b << '\n';
// b *= 2; // Error: b is not of arithmetic type
b <<= 1;
std::cout << "2. " << b << '\n';
b >>= 1;
std::cout << "3. " << b << '\n';
std::cout << "4. " << (b << 1) << '\n';
std::cout << "5. " << (b >> 1) << '\n';
b |= std::byte{0b11110000};
std::cout << "6. " << b << '\n';
b &= std::byte{0b11110000};
std::cout << "7. " << b << '\n';
b ^= std::byte{0b11111111};
std::cout << "8. " << b << '\n';
}
π Output π
1. 00101010
2. 01010100
3. 00101010
4. 01010100
5. 00010101
6. 11111010
7. 11110000
8. 00001111
mwah π thatβs the whole page, bestie β source is http://cppreference.com, date stamp is 2024.06.10, and the page is std::byte(3). Go forth and flex those bits responsibly. π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms