Utility Rack
Loading...
Searching...
No Matches
byte_array.hpp File Reference

Utility functions to construct and compare a std::array of std::byte objects. More...

#include <array>
#include <cstddef>
#include <utility>
Include dependency graph for byte_array.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

template<typename... Ts>
constexpr std::array< std::byte, sizeof...(Ts)> chops::make_byte_array (Ts &&... args) noexcept
 
template<typename std::size_t N>
constexpr bool chops::compare_byte_arrays (const std::array< std::byte, N > &lhs, const std::array< std::byte, N > &rhs)
 

Detailed Description

Utility functions to construct and compare a std::array of std::byte objects.

A std::byte is defined as an enum and there are no implicit conversions from int to an enum class. Instead of writing:

std::array<std::byte, 5> arr =
{ std::byte{0x36}, std::byte{0xd0}, std::byte{0x42}, std::byte{0xbe}, std::byte{0xef} };

it is easier (specially for larger arrays) to write:

auto arr = chops::make_byte_array(0x36, 0xd0, 0x42, 0xbe, 0xef);

The make_byte_array function is taken from an example by Blitz Rakete on Stackoverflow (Blitz is user Rakete1111 on SO). The static_cast was added to eliminate "narrowing" warnings (since typically integer values are used to initialize the std::byte values, which would be a narrowing conversion).

The compare_byte_arrays function simplifies test code, typically using the Catch2 unit test library.

Authors
Blitz Rakete, Cliff Green

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)