std::byte
From 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以降) |
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初期化規則により、数値 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