Namespaces
Variants

std:: byte

From cppreference.net
Utilities library
定義済みヘッダー <cstddef>
enum class byte : unsigned char { } ;
(C++17以降)

std::byte は、C++言語定義で規定されているバイトの概念を実装する固有の型です。

unsigned char と同様に、他のオブジェクトが占有する生メモリにアクセスするために使用できます( object representation )が、 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 ) ) ;

注記

数値 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