Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::byte

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
      Defined in header<cstddef>
      enumclass byte:unsignedchar{};
      (since C++17)

      std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.

      Likeunsignedchar, it can be used to access raw memory occupied by other objects (object representation), but unlikeunsignedchar, it is not a character type and is not an arithmetic type.std::byte models a mere collection of bits, supporting only bitshift operations with an integer, and bitwise and comparison operations with anotherstd::byte.

      Contents

      [edit]Non-member functions

      std::to_integer

      template<class IntegerType>
      constexpr IntegerType to_integer( std::byte b)noexcept;
      (since C++17)

      Equivalent to:return IntegerType(b);This overload participates in overload resolution only ifstd::is_integral_v<IntegerType> istrue.

      std::operator<<=,operator>>=

      template<class IntegerType>
      constexpr std::byte& operator<<=( std::byte& b, IntegerType shift)noexcept;
      (1)(since C++17)
      template<class IntegerType>
      constexpr std::byte& operator>>=( std::byte& b, IntegerType shift)noexcept;
      (2)(since C++17)
      1) Equivalent to:return b= b<< shift;This overload participates in overload resolution only ifstd::is_integral_v<IntegerType> istrue.
      2) Equivalent to:return b= b>> shift;

      This overload participates in overload resolution only ifstd::is_integral_v<IntegerType> istrue.

      std::operator<<,operator>>

      template<class IntegerType>
      constexpr std::byte operator<<( std::byte b, IntegerType shift)noexcept;
      (1)(since C++17)
      template<class IntegerType>
      constexpr std::byte operator>>( std::byte b, IntegerType shift)noexcept;
      (2)(since C++17)
      1) Equivalent to:return std::byte(static_cast<unsignedint>(b)<< shift);This overload participates in overload resolution only ifstd::is_integral_v<IntegerType> istrue.
      2) Equivalent to:return std::byte(static_cast<unsignedint>(b)>> shift);

      This overload participates in overload resolution only ifstd::is_integral_v<IntegerType> istrue.

      std::operator|=,operator&=,operator^=

      constexpr std::byte& operator|=( std::byte& l, std::byte r)noexcept;
      (1)(since C++17)
      constexpr std::byte& operator&=( std::byte& l, std::byte r)noexcept;
      (2)(since C++17)
      constexpr std::byte& operator^=( std::byte& l, std::byte r)noexcept;
      (3)(since C++17)
      1) Equivalent to:return l= l| r;.
      2) Equivalent to:return l= l& r;.
      3) Equivalent to:return l= l^ r;.

      std::operator|,operator&,operator^,operator~

      constexpr std::byte operator|( std::byte l, std::byte r)noexcept;
      (1)(since C++17)
      constexpr std::byte operator&( std::byte l, std::byte r)noexcept;
      (2)(since C++17)
      constexpr std::byte operator^( std::byte l, std::byte r)noexcept;
      (3)(since C++17)
      constexpr std::byte operator~( std::byte b)noexcept;
      (4)(since C++17)
      1) Equivalent to:return std::byte(static_cast<unsignedint>(l)|static_cast<unsignedint>(r));.
      2) Equivalent to:return std::byte(static_cast<unsignedint>(l)&static_cast<unsignedint>(r));.
      3) Equivalent to:return std::byte(static_cast<unsignedint>(l)^static_cast<unsignedint>(r));.
      4) Equivalent to:return std::byte(~static_cast<unsignedint>(b));

      [edit]Notes

      A numeric valuen can be converted to a byte value usingstd::byte{n}, due to C++17relaxed enum class initialization rules.

      A byte can be converted to a numeric value (such as to produce an integer hash of an object) the usual way with anexplicit conversion or alternatively withstd::to_integer.

      Feature-test macroValueStdFeature
      __cpp_lib_byte201603L(C++17)std::byte

      [edit]Example

      Run this code
      #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);}

      Output:

      00101010
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/byte&oldid=173341"

      [8]ページ先頭

      ©2009-2025 Movatter.jp