My Project
Public Types | Public Member Functions | Related Functions | List of all members
bsl::byte Class Referencefinal

std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition. Shift operations all require unsigned integer types, instead of any integer type. More...

#include <byte.hpp>

Public Types

using value_type = bsl::uint8
 alias for: T
 

Public Member Functions

 byte () noexcept=default
 Default constructor. This ensures the byte type is a POD type, allowing it to be constructed as a global resource. This is needed as aligned storage uses a bsl::byte as its base type, and aligned storage is needed as a global resource to support the bsl::manager. More...
 
constexpr byte (value_type const val) noexcept
 Creates a bsl::byte from a value_type. More...
 
template<typename T = value_type, enable_if_t< is_integral< T >::value, bool > = true>
constexpr T to_integer () const noexcept
 Returns the bsl::byte as a given integer type using a static_cast to perform the conversion. More...
 
 ~byte () noexcept=default
 Destroyes a previously created bsl::byte.
 
constexpr byte (byte const &o) noexcept=default
 copy constructor More...
 
constexpr byte (byte &&o) noexcept=default
 move constructor More...
 
constexpr byteoperator= (byte const &o) &noexcept=default
 copy assignment More...
 
constexpr byteoperator= (byte &&o) &noexcept=default
 move assignment More...
 
template<typename O >
 byte (O val) noexcept=delete
 This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it. More...
 

Related Functions

(Note that these are not member functions.)

constexpr bool operator== (byte const &lhs, byte const &rhs) noexcept
 The same as lhs.to_integer() == rhs.to_integer() More...
 
constexpr bool operator!= (byte const &lhs, byte const &rhs) noexcept
 The same as !(lhs == rhs) More...
 
constexpr byteoperator<<= (byte &b, bsl::uint32 const shift) noexcept
 The same as b = byte{b.to_integer() << shift}. More...
 
constexpr byteoperator>>= (byte &b, bsl::uint32 const shift) noexcept
 The same as b = byte{b.to_integer() >> shift}. More...
 
constexpr byte operator<< (byte const &b, bsl::uint32 const shift) noexcept
 The same as byte tmp{b}; tmp <<= shift;. More...
 
constexpr byte operator>> (byte const &b, bsl::uint32 const shift) noexcept
 The same as byte tmp{b}; tmp >>= shift;. More...
 
constexpr byteoperator|= (byte &lhs, byte const &rhs) noexcept
 The same as lhs = byte{lhs.to_integer() | rhs.to_integer()};. More...
 
constexpr byteoperator&= (byte &lhs, byte const &rhs) noexcept
 The same as lhs = byte{lhs.to_integer() & rhs.to_integer()};. More...
 
constexpr byteoperator^= (byte &lhs, byte const &rhs) noexcept
 The same as lhs = byte{lhs.to_integer() ^ rhs.to_integer()};. More...
 
constexpr byte operator| (byte const &lhs, byte const &rhs) noexcept
 The same as tmp{lhs}; tmp |= rhs;. More...
 
constexpr byte operator & (byte const &lhs, byte const &rhs) noexcept
 The same as tmp{lhs}; tmp &= rhs;. More...
 
constexpr byte operator^ (byte const &lhs, byte const &rhs) noexcept
 The same as tmp{lhs}; tmp ^= rhs;. More...
 
constexpr byte operator~ (byte const &b) noexcept
 The same as byte{~b.to_integer()}. More...
 

Detailed Description

std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition. Shift operations all require unsigned integer types, instead of any integer type.

#include <bsl/byte.hpp>
#include <bsl/debug.hpp>
namespace bsl
{
inline void
example_byte_overview() noexcept
{
bsl::byte const mybyte{static_cast<bsl::uint8>(42)};
bsl::print() << "success: " << mybyte.to_integer();
}
}

Constructor & Destructor Documentation

◆ byte() [1/5]

bsl::byte::byte ( )
defaultnoexcept

Default constructor. This ensures the byte type is a POD type, allowing it to be constructed as a global resource. This is needed as aligned storage uses a bsl::byte as its base type, and aligned storage is needed as a global resource to support the bsl::manager.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_default_constructor() noexcept
{
bsl::byte b{};
constexpr bsl::uint8 val{0x10U};
constexpr bsl::uint8 expected{0x10U};
b = bsl::byte{val};
if (b.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}

◆ byte() [2/5]

constexpr bsl::byte::byte ( value_type const  val)
inlinenoexcept

Creates a bsl::byte from a value_type.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_by_value_constructor() noexcept
{
constexpr bsl::uint8 val{0x10U};
constexpr bsl::uint8 expected{0x10U};
bsl::byte const b{val};
if (b.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}

SUPPRESSION: PRQA 2180 - exception required

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is a fundamental type, but all implicit conversions are disabled through the use of the implicit general template constructor that is deleted which absorbs all incoming potential implicit conversions.
Parameters
valthe value of the integer to create the bsl::byte from.

◆ byte() [3/5]

constexpr bsl::byte::byte ( byte const &  o)
defaultnoexcept

copy constructor

Parameters
othe object being copied

◆ byte() [4/5]

constexpr bsl::byte::byte ( byte &&  o)
defaultnoexcept

move constructor

Parameters
othe object being moved

◆ byte() [5/5]

template<typename O >
bsl::byte::byte ( val)
deletenoexcept

This constructor allows for single argument constructors without the need to mark them as explicit as it will absorb any incoming potential implicit conversion and prevent it.

SUPPRESSION: PRQA 2180 - false positive

  • We suppress this because A12-1-4 states that all constructors that are callable from a fundamental type should be marked as explicit. This is callable with a fundamental type, but it is marked as "delete" which means it does not apply.
Template Parameters
Othe type that could be implicitly converted
Parameters
valthe value that could be implicitly converted

Member Function Documentation

◆ to_integer()

template<typename T = value_type, enable_if_t< is_integral< T >::value, bool > = true>
constexpr T bsl::byte::to_integer ( ) const
inlinenoexcept

Returns the bsl::byte as a given integer type using a static_cast to perform the conversion.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_to_integer() noexcept
{
constexpr bsl::uint8 val{0x10U};
constexpr bsl::uint8 expected{0x10U};
bsl::byte const b{val};
if (b.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Template Parameters
Tthe type of integer to convert the bsl::byte to
Returns
Returns the bsl::byte as a given integer type using a static_cast to perform the conversion.

◆ operator=() [1/2]

constexpr byte& bsl::byte::operator= ( byte const &  o) &
defaultnoexcept

copy assignment

Parameters
othe object being copied
Returns
a reference to *this

◆ operator=() [2/2]

constexpr byte& bsl::byte::operator= ( byte &&  o) &
defaultnoexcept

move assignment

Parameters
othe object being moved
Returns
a reference to *this

Friends And Related Function Documentation

◆ operator==()

constexpr bool operator== ( byte const &  lhs,
byte const &  rhs 
)
related

The same as lhs.to_integer() == rhs.to_integer()

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_equal() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x10U};
bsl::byte const b1{val1};
bsl::byte const b2{val2};
if (b1 == b2) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
returns lhs.to_integer() == rhs.to_integer()

◆ operator!=()

constexpr bool operator!= ( byte const &  lhs,
byte const &  rhs 
)
related

The same as !(lhs == rhs)

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_not_equal() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
bsl::byte const b1{val1};
bsl::byte const b2{val2};
if (b1 != b2) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the operator
rhsthe right hand side of the operator
Returns
returns !(lhs == rhs)

◆ operator<<=()

constexpr byte & operator<<= ( byte b,
bsl::uint32 const  shift 
)
related

The same as b = byte{b.to_integer() << shift}.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_lshift_assign() noexcept
{
constexpr bsl::uint8 val{0x01U};
constexpr bsl::uint8 expected{0x02U};
bsl::byte b{val};
b <<= 1U;
if (b.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
bthe bsl::byte to shift
shiftthe number of bits to shift b
Returns
returns a reference to the provided "b"

◆ operator>>=()

constexpr byte & operator>>= ( byte b,
bsl::uint32 const  shift 
)
related

The same as b = byte{b.to_integer() >> shift}.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_rshift_assign() noexcept
{
constexpr bsl::uint8 val{0x02U};
constexpr bsl::uint8 expected{0x01U};
bsl::byte b{val};
b >>= 1U;
if (b.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
bthe bsl::byte to shift
shiftthe number of bits to shift b
Returns
returns a reference to the provided "b"

◆ operator<<()

constexpr byte operator<< ( byte const &  b,
bsl::uint32 const  shift 
)
related

The same as byte tmp{b}; tmp <<= shift;.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_lshift() noexcept
{
constexpr bsl::uint8 val{0x01U};
constexpr bsl::uint8 expected{0x02U};
bsl::byte const b{val};
if ((b << 1U).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
bthe bsl::byte to shift
shiftthe number of bits to shift b
Returns
returns byte tmp{b}; tmp <<= shift;

◆ operator>>()

constexpr byte operator>> ( byte const &  b,
bsl::uint32 const  shift 
)
related

The same as byte tmp{b}; tmp >>= shift;.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_rshift() noexcept
{
constexpr bsl::uint8 val{0x02U};
constexpr bsl::uint8 expected{0x01U};
bsl::byte const b{val};
if ((b >> 1U).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
bthe bsl::byte to shift
shiftthe number of bits to shift b
Returns
returns byte tmp{b}; tmp >>= shift;

◆ operator|=()

constexpr byte & operator|= ( byte lhs,
byte const &  rhs 
)
related

The same as lhs = byte{lhs.to_integer() | rhs.to_integer()};.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_or_assign() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x11U};
bsl::byte b1{val1};
bsl::byte const b2{val2};
b1 |= b2;
if (b1.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns a reference to the provided "lhs"

◆ operator&=()

constexpr byte & operator&= ( byte lhs,
byte const &  rhs 
)
related

The same as lhs = byte{lhs.to_integer() & rhs.to_integer()};.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_and_assign() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x10U};
bsl::byte b1{val1};
bsl::byte const b2{val2};
b1 &= b2;
if (b1.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns a reference to the provided "lhs"

◆ operator^=()

constexpr byte & operator^= ( byte lhs,
byte const &  rhs 
)
related

The same as lhs = byte{lhs.to_integer() ^ rhs.to_integer()};.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_xor_assign() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x01U};
bsl::byte b1{val1};
bsl::byte const b2{val2};
b1 ^= b2;
if (b1.to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns a reference to the provided "lhs"

◆ operator|()

constexpr byte operator| ( byte const &  lhs,
byte const &  rhs 
)
related

The same as tmp{lhs}; tmp |= rhs;.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_or() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x11U};
bsl::byte const b1{val1};
bsl::byte const b2{val2};
if ((b1 | b2).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns tmp{lhs}; tmp |= rhs;

◆ operator &()

constexpr byte operator & ( byte const &  lhs,
byte const &  rhs 
)
related

The same as tmp{lhs}; tmp &= rhs;.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_and() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x10U};
bsl::byte const b1{val1};
bsl::byte const b2{val2};
if ((b1 & b2).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns tmp{lhs}; tmp &= rhs;

◆ operator^()

constexpr byte operator^ ( byte const &  lhs,
byte const &  rhs 
)
related

The same as tmp{lhs}; tmp ^= rhs;.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_xor() noexcept
{
constexpr bsl::uint8 val1{0x10U};
constexpr bsl::uint8 val2{0x11U};
constexpr bsl::uint8 expected{0x01U};
bsl::byte const b1{val1};
bsl::byte const b2{val2};
if ((b1 ^ b2).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
lhsthe left hand side of the binary operation
rhsthe right hand side of the binary operation
Returns
returns tmp{lhs}; tmp ^= rhs;

◆ operator~()

constexpr byte operator~ ( byte const &  b)
related

The same as byte{~b.to_integer()}.

#include <bsl/debug.hpp>
#include <bsl/byte.hpp>
namespace bsl
{
inline void
example_byte_complement() noexcept
{
constexpr bsl::uint8 val{0x10U};
constexpr bsl::uint8 expected{static_cast<bsl::uint8>(0xEF)};
bsl::byte const b{val};
if ((~b).to_integer() == expected) {
bsl::print() << "success\n";
}
}
}
Parameters
bthe bsl::byte to invert
Returns
returns byte{~b.to_integer()}

The documentation for this class was generated from the following file: