命名空间
变体
操作

std::byte

来自 cppreference.cn
< cpp‎ | types
 
 
 
 
定义于头文件 <cstddef>
enum class byte : unsigned char {};
(自 C++17 起)

std::byte 是一种独特的类型,它实现了 C++ 语言定义中指定的 byte 的概念。

unsigned char 类似,它可以用于访问其他对象占用的原始内存 (对象表示),但与 unsigned char 不同,它不是字符类型,也不是算术类型。 std::byte 模拟了一个纯粹的位集合,仅支持与整数的位移操作,以及与另一个 std::byte 的位运算和比较运算。

内容

[编辑] 非成员函数

std::to_integer

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

等价于: return IntegerType(b); 仅当 std::is_integral_v<IntegerType>true 时,此重载参与重载解析。

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

template< class IntegerType >
constexpr std::byte& operator<<=( std::byte& b, IntegerType shift ) noexcept;
(1) (自 C++17 起)
template< class IntegerType >
constexpr std::byte& operator>>=( std::byte& b, IntegerType shift ) noexcept;
(2) (自 C++17 起)
1) 等价于: return b = b << shift; 仅当 std::is_integral_v<IntegerType>true 时,此重载参与重载解析。
2) 等价于: return b = b >> shift;

仅当 std::is_integral_v<IntegerType>true 时,此重载参与重载解析。

std::operator<<,operator>>

template< class IntegerType >
constexpr std::byte operator<<( std::byte b, IntegerType shift ) noexcept;
(1) (自 C++17 起)
template< class IntegerType >
constexpr std::byte operator>>( std::byte b, IntegerType shift ) noexcept;
(2) (自 C++17 起)
1) 等价于: return std::byte(static_cast<unsigned int>(b) << shift); 仅当 std::is_integral_v<IntegerType>true 时,此重载参与重载解析。
2) 等价于: return std::byte(static_cast<unsigned int>(b) >> shift);

仅当 std::is_integral_v<IntegerType>true 时,此重载参与重载解析。

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

constexpr std::byte& operator|=( std::byte& l, std::byte r ) noexcept;
(1) (自 C++17 起)
constexpr std::byte& operator&=( std::byte& l, std::byte r ) noexcept;
(2) (自 C++17 起)
constexpr std::byte& operator^=( std::byte& l, std::byte r ) noexcept;
(3) (自 C++17 起)
1) 等价于: return l = l | r;.
2) 等价于: return l = l & r;.
3) 等价于: return l = l ^ r;.

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

constexpr std::byte operator|( std::byte l, std::byte r ) noexcept;
(1) (自 C++17 起)
constexpr std::byte operator&( std::byte l, std::byte r ) noexcept;
(2) (自 C++17 起)
constexpr std::byte operator^( std::byte l, std::byte r ) noexcept;
(3) (自 C++17 起)
constexpr std::byte operator~( std::byte b ) noexcept;
(4) (自 C++17 起)
1) 等价于: return std::byte(static_cast<unsigned int>(l) | static_cast<unsigned int>(r));.
2) 等价于: return std::byte(static_cast<unsigned int>(l) & static_cast<unsigned int>(r));.
3) 等价于: return std::byte(static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));.
4) 等价于: return std::byte(~static_cast<unsigned int>(b));

[编辑] 注释

由于 C++17 放宽的枚举类初始化 规则,数值 n 可以使用 std::byte{n} 转换为 byte 值。

byte 可以使用 std::to_integer 或通过 显式转换 以通常方式转换为数值(例如,生成对象的整数哈希)。

特性测试 标准 特性
__cpp_lib_byte 201603L (C++17) std::byte

[编辑] 示例

#include <bitset>
#include <cassert>
#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
 
    auto to_int = [](std::byte b) { return std::to_integer<int>(b); };
 
    std::byte b{42};
    assert(to_int(b) == 0b00101010);
    std::cout << b << '\n';
 
    // b *= 2; // Error: b is not of arithmetic type
    b <<= 1;
    assert(to_int(b) == 0b01010100);
 
    b >>= 1;
    assert(to_int(b) == 0b00101010);
 
    assert(to_int(b << 1) == 0b01010100);
    assert(to_int(b >> 1) == 0b00010101);
 
    b |= std::byte{0b11110000};
    assert(to_int(b) == 0b11111010);
 
    b &= std::byte{0b11110000};
    assert(to_int(b) == 0b11110000);
 
    b ^= std::byte{0b11111111};
    assert(to_int(b) == 0b00001111);
}

输出

00101010