std::byte
來自 cppreference.com
定義於標頭檔案 <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