Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
utility: Low level utility facilities

Arene Base provides backports of facilities from <utility> in newer C++ versions, along with other similar low-level facilities.

The public header is

Public export header for content from arene/base/utility.

The Bazel target is

//:utility

Utility Facilities

The utility package provides the following basic facilities:

Alignment

  • arene::base::is_aligned: check if an integer or pointer is aligned to the specified power of 2.
  • arene::base::align_floor: adjust an integer to the nearest multiple of the specified alignment that is less than or equal to the supplied value
  • arene::base::align_ceil: adjust an integer to the nearest multiple of the specified alignment that is greater than or equal to the supplied value
  • arene::base::alignment_offset: determine the number of bytes by which a pointer would need to be adjusted to be aligned to the specified value

Bit Masks

Creating Const Reference To A Non-Const Object

  • arene::base::as_const(foo) returns a const reference to foo. This is a back-port of std::as_const

Forwarding A Reference Similarly To Another Type

arene::base::forward_like provides a backport of C++23's std::forward_like. This is useful in implementing deducing this style helpers, where the const and r/l-value reference qualifications of the Self parameter need to be propagated to the type of other values.

Tags For In-place Construction

Creating A Range From An Iterator Pair

template<typename Iterator>
void foo(Iterator first, Iterator last) {
for(auto& element: arene::base::make_subrange(first, last)) {
do_stuff(element);
}
}
constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(std::is_nothrow_move_constructible< Iterator >::value &&//std::is_nothrow_move_constructible< Sentinel >::value) -> make_subrange_detail::iterator_range< Iterator, Sentinel >
Combine an iterator and sentinel into a range type that can be used with range-based for loops.
Definition make_subrange.hpp:85

Swapping Values

arene::base::swap provides a constexpr version of std::swap, which is also implemented as a Customization Point Object. As specified in the linked paper, doing so allows a few of advantages:

  • It avoids the user having to remember to do the "ADL two-step" of needing to do using arene::base::swap; before an unqualified swap call: Invoking arene::base::swap(T&,T&) will find ADL-specializations of swap(T&, T&), or fall back to the default implementation if no customizations are found.
  • It allows consistent concept enforcement. The arene::base::is_swappable_v<T> trait is checked up front, rather than a user seeing a potentially confusing failure if ADL lookup fails and the type is not default-swappable.
  • It allows swap to be passed directly to algorithms as a function object.

Converting An Enum To Its Underlying Value

Safe Integer Comparison

Backports of the C++20 safe integer comparison facilities are provided. These perform integer comparisons without performing any implicit conversions on the arguments, thereby avoiding issues such as negative values implicitly wrapping around to large positive values. For example, using raw comparison operators 0 < 5u == true as expected, but -1 < 5u == false. Using the equivalent safe comparison instead, arene::base::cmp_less(0, 5u) == true and arene::base::cmp_less(-1, 5u) == true as one would expect.

The available functions are as follows:

utility corresponding operator C++20 stdlib equivalent
arene::base::cmp_equal operator== std::cmp_equal
arene::base::cmp_not_equal operator!= std::cmp_not_equal
arene::base::cmp_less operator< std::cmp_less
arene::base::cmp_greater operator> std::cmp_greater
arene::base::cmp_less_equal operator<= std::cmp_less_equal
arene::base::cmp_greater_equal operator>= std::cmp_greater_equal