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) | (C++17以降) |
|
template
<
class
IntegerType
>
constexpr std :: byte & operator >>= ( std :: byte & b, IntegerType shift ) noexcept ; |
(2) | (C++17以降) |
このオーバーロードは、 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:: 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}; // 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