std::byte
来自cppreference.com
在标头 <cstddef> 定义 | ||
enumclass byte :unsignedchar{}; | (C++17 起) | |
std::byte
是一种独立类型,它实现了 C++ 语言定义中所规定的字节的概念。
类似 unsignedchar,它能用于访问其他对象所占据的原始内存(对象表示),但不同于 unsignedchar,它不是字符类型也不是算术类型。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<unsignedint>(b)<< shift); 此重载只有在 std::is_integral_v<IntegerType> 为 true 时才会参与重载决议。
2) 等价于:return std::byte(static_cast<unsignedint>(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<unsignedint>(l)|static_cast<unsignedint>(r));。
2) 等价于:return std::byte(static_cast<unsignedint>(l)&static_cast<unsignedint>(r));。
3) 等价于:return std::byte(static_cast<unsignedint>(l)^static_cast<unsignedint>(r));。
4) 等价于:return std::byte(~static_cast<unsignedint>(b));。
[编辑]注解
由于 C++17 放松的 enum class 初始化规则,可以用 std::byte{n} 转换数值 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; // 错误:无法把 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