std:: byte
| 
           定义于头文件
            
            
             <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) | (since C++17) | 
| 
            
             
              template
             
             
              <
             
             
              class
             
             IntegerType
             
              >
             
              constexpr std :: byte & operator >>= ( std :: byte & b, IntegerType shift ) noexcept ; | (2) | (since C++17) | 
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) | 
此重载仅当 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 起) | 
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 起) | 
注释
数值 n 可通过 std :: byte { n } 转换为字节值,这得益于 C++17 的 放宽枚举类初始化 规则。
       字节可以通过常规方式转换为数值(例如生成对象的整数哈希值),使用
       
        显式转换
       
       ,或者使用
       
        
         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}; // 正确 // if (y == 13) {} // 错误:无法进行比较 if (y == std::byte{13}) {} // 正确,字节可比较 int arr[]{1, 2, 3}; // int c = a[y]; // 错误:数组下标不是整数 [[maybe_unused]] int i = arr[std::to_integer<int>(y)]; // 正确 [[maybe_unused]] int j = arr[std::to_underlying(y)]; // 正确 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