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 은 C++17의 열거형 클래스 완화된 초기화 규칙으로 인해 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; // 오류: 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