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 } благодаря правилам ослабленной инициализации enum class в 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, байты сравниваемы 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