Namespaces
Variants

std:: byte

From cppreference.net
Utilities library
Definido en el encabezado <cstddef>
enum class byte : unsigned char { } ;
(desde C++17)

std::byte es un tipo distinto que implementa el concepto de byte según se especifica en la definición del lenguaje C++.

Al igual que unsigned char , puede utilizarse para acceder a la memoria sin procesar ocupada por otros objetos ( representación de objetos ), pero a diferencia de unsigned char , no es un tipo de carácter ni es un tipo aritmético. std::byte modela una mera colección de bits, que solo admite operaciones de desplazamiento de bits con un entero, y operaciones bit a bit y de comparación con otro std::byte .

Contenidos

Funciones no miembro

std:: to_integer

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

Equivalente a: return IntegerType ( b ) ; Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_integral_v < IntegerType > es true .

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

template < class IntegerType >
constexpr std :: byte & operator <<= ( std :: byte & b, IntegerType shift ) noexcept ;
(1) (desde C++17)
template < class IntegerType >
constexpr std :: byte & operator >>= ( std :: byte & b, IntegerType shift ) noexcept ;
(2) (desde C++17)
1) Equivalente a: return b = b << shift ; Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_integral_v < IntegerType > es true .
2) Equivalente a: return b = b >> shift ;

Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_integral_v < IntegerType > es true .

std:: operator<<,operator>>

template < class IntegerType >
constexpr std :: byte operator << ( std :: byte b, IntegerType shift ) noexcept ;
(1) (desde C++17)
template < class IntegerType >
constexpr std :: byte operator >> ( std :: byte b, IntegerType shift ) noexcept ;
(2) (desde C++17)
1) Equivalente a: return std :: byte ( static_cast < unsigned int > ( b ) << shift ) ; Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_integral_v < IntegerType > es true .
2) Equivalente a: return std :: byte ( static_cast < unsigned int > ( b ) >> shift ) ;

Esta sobrecarga participa en la resolución de sobrecarga solo si std:: is_integral_v < IntegerType > es true .

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

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

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

constexpr std :: byte operator | ( std :: byte l, std :: byte r ) noexcept ;
(1) (desde C++17)
constexpr std :: byte operator & ( std :: byte l, std :: byte r ) noexcept ;
(2) (desde C++17)
constexpr std :: byte operator ^ ( std :: byte l, std :: byte r ) noexcept ;
(3) (desde C++17)
constexpr std :: byte operator~ ( std :: byte b ) noexcept ;
(4) (desde C++17)
1) Equivalente a: return std :: byte ( static_cast < unsigned int > ( l ) | static_cast < unsigned int > ( r ) ) ; .
2) Equivalente a: return std :: byte ( static_cast < unsigned int > ( l ) & static_cast < unsigned int > ( r ) ) ; .
3) Equivalente a: return std :: byte ( static_cast < unsigned int > ( l ) ^ static_cast < unsigned int > ( r ) ) ; .
4) Equivalente a: return std :: byte ( ~ static_cast < unsigned int > ( b ) ) ;

Notas

Un valor numérico n puede convertirse a un valor byte usando std :: byte { n } , gracias a las reglas de inicialización relajada de enum class de C++17.

Un byte puede convertirse a un valor numérico (como para producir un hash entero de un objeto) de la manera habitual con una conversión explícita o alternativamente con std::to_integer .

Macro de prueba de características Valor Std Característica
__cpp_lib_byte 201603L (C++17) std::byte

Ejemplo

#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: no se puede convertir int a byte.
    std::byte y{1}; // OK
    // if (y == 13) {} // Error: no se puede comparar.
    if (y == std::byte{13}) {} // OK, los bytes son comparables
    int arr[]{1, 2, 3};
    // int c = a[y]; // Error: el subíndice del array no es un entero
    [[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 no es de tipo aritmético
    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);
}

Salida:

00101010