std::byte

来自cppreference.com
< cpp‎ | types
 
 
工具库
语言支持
类型支持(基本类型、RTTI)
库功能特性测试宏 (C++20)
动态内存管理
程序工具
协程支持 (C++20)
变参数函数
调试支持
(C++26)
三路比较
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)   
(C++20)
交换类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)

 
类型支持
基本类型
定宽整数类型 (C++11)
定宽浮点类型 (C++23)
byte
(C++17)
数值极限
C 数值极限接口
运行时类型信息
 
在标头 <cstddef> 定义
enum class byte : unsigned char {};
(C++17 起)

std::byte 是一种独立类型,它实现了 C++ 语言定义中所规定的字节的概念。

类似 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 放松的 enum class 初始化规则,可以用 std::byte{n} 转换数值 nbyte 值。

可以使用常规方式用显式转换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; // 错误:无法把 int 转换为 byte。
    std::byte y{1}; // OK
 
    // if (y == 13) {} // 错误:无法比较。
    if (y == std::byte{13}) {} // OK,byte 是可比较的
 
    int arr[]{1, 2, 3};
    // int c = a[y]; // 错误:数组下标不是整数
    [[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; // 错误:b 不是算术类型
    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