std::byte
来自 cppreference.cn
定义于头文件 <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 起) |
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} 转换为字节值。
字节可以通过 显式转换 或 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