Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
ranges/type_traits: Range Type Traits

The ranges/type_traits subpackage provides C++14-compatible type traits for properties described by concepts in std::ranges.

The public export header is:

#include "arene/base/stdlib_choice/integral_constant.hpp"

enable_borrowed_range

arene::base::ranges::enable_borrowed_range<Rng> is an opt-in trait and a backport of std::ranges::enable_borrowed_range. It indicates that iterators obtained from a range remain valid after the range object is destroyed. The primary template defaults to false.

The trait should be specialized to be true only for a range type that has this lifetime property. The trait records this property; it does not provide the range's iterator operations.

The following example defines an owning range and a range that refers to storage owned elsewhere:

/// @brief A range whose iterators refer to storage owned by the range.
struct owning_range {
auto begin() const noexcept -> std::array<int, 3>::const_iterator { return values_.begin(); }
auto end() const noexcept -> std::array<int, 3>::const_iterator { return values_.end(); }
private:
std::array<int, 3> values_{};
};
/// @brief A generic range whose iterators refer to storage owned elsewhere.
template <typename T>
class user_borrowed_range {
public:
explicit user_borrowed_range(T const* first, T const* last) noexcept
: first_(first),
last_(last) {}
auto begin() const noexcept -> T const* { return first_; }
auto end() const noexcept -> T const* { return last_; }
private:
T const* first_;
T const* last_;
};

Only the range whose iterators do not depend on the lifetime of the range object is opted in:

/// @brief A user specialization of @c enable_borrowed_range which is unconditionally a borrowed range.
/// @note The specialization must be contained in the @c arene::base::ranges namespace.
/// This means doing it at global scope if done using an FQN like this example, or by explicitly opening
/// the @c arene::base::ranges namespace and placing it inside it.
template <typename T>
class arene::base::ranges::enable_borrowed_range<ranges_examples::user_borrowed_range<T>> : public std::true_type {};

The class-trait and variable-template forms can then be queried at compile time:

static_assert(!ranges::enable_borrowed_range_v<owning_range>, "Owning ranges are not borrowed");
static_assert(ranges::enable_borrowed_range_v<user_borrowed_range<int>>, "The borrowed range is opted in");
static_assert(ranges::enable_borrowed_range_v<user_borrowed_range<int>>, "The borrowed range is opted in");

Note that users should rarely query enabled_borrowed_range directly. Instead prefer arene::base::is_borrowed_range<Rng>.

The trait is used by arene::base::ranges::begin and arene::base::ranges::end to determine whether range access is permitted for an rvalue; see ranges/range_access: Range Access .

disable_sized_range

arene::base::ranges::disable_sized_range<Rng> is an opt-out trait and a backport of std::ranges::disable_sized_range. It indicates that a range type's member or ADL size() is not a constant-time operation and so must not be used to compute the range's size. It is a class trait whose primary template derives from std::false_type.

Unlike the standard variable-template customization point, disable_sized_range is a struct because C++14 lacks inline variables, which makes explicit specialization of a variable template across translation units error-prone.

The trait should be specialized to derive from std::true_type only for a range type whose size() cannot be assumed to run in constant time, for example one that must count the elements of a singly-linked list:

/// @brief A range whose @c size() delegates to a singly-linked list and so is not constant-time.
///
/// @c std::forward_list has no @c size() member precisely because counting its elements is a linear-time
/// operation, so a range that exposes one advertises a @c size() that should not be treated as constant-time.
struct forward_list_range {
auto size() const noexcept -> std::size_t {
return static_cast<std::size_t>(std::distance(values_.begin(), values_.end()));
}
private:
std::forward_list<int> values_{};
};

Such a range is opted out so that its non-constant-time size() is disregarded:

/// @brief A user opt-out of the constant-time assumption for a range with a linear-time @c size() .
/// @note The specialization derives from @c std::true_type to opt out. It may be declared in any namespace enclosing
/// @c arene::base::ranges ; here it uses a fully-qualified name at global scope.
template <>
struct arene::base::ranges::disable_sized_range<ranges_examples::forward_list_range> : std::true_type {};

The variable template arene::base::ranges::disable_sized_range_v<Rng> provides the same query as disable_sized_range<Rng>::value in a concise form, and can be queried at compile time:

struct constant_time_size_range {};
static_assert(
"A range with a constant-time size() is not opted out"
);
static_assert(
"The range with a linear-time size() is opted out"
);

is_range

arene::base::ranges::is_range<Rng> is a compile-time query for whether Rng is a range suitable for use in range algorithms/views/etc. It is a backport of std::ranges::range. As with the range access functions, the cv- and reference-qualification of Rng is significant. For example, an lvalue reference to a non-borrowed range can satisfy is_range, while the unqualified range type does not because it would be accessed as an rvalue.

The trait has a static member value which is equal to true if and only if:

  1. arene::base::ranges::begin(std::declval<Rng>()) is well-formed.
  2. arene::base::ranges::end(std::declval<Rng>()) is well-formed.

The variable template arene::base::ranges::is_range_v<Rng> provides the same query as is_range<Rng>::value in a concise form.

The following examples show valid standard-library and user-defined ranges, as well as types which do not satisfy the complete range requirements:

/// @brief A type with no range access operations.
struct not_a_range {};
/// @brief A type whose accessors do not return an iterator pair as defined by @c denotes_range .
struct invalid_range {
static auto begin() noexcept -> int { return 0; }
static auto end() noexcept -> int { return 0; }
};
static_assert(ranges::is_range_v<std::array<int, 3>&>, "std::array is a range");
static_assert(ranges::is_range_v<owning_range&>, "A non-borrowed range lvalue is a range");
static_assert(!ranges::is_range_v<owning_range&&>, "A non-borrowed range rvalue is not a range");
static_assert(ranges::is_range_v<user_borrowed_range<int>&>, "A borrowed range lvalue is a range");
static_assert(ranges::is_range_v<user_borrowed_range<int>&&>, "A borrowed range rvalue is a range");
static_assert(!ranges::is_range_v<not_a_range&>, "A type without accessors is not a range");
static_assert(!ranges::is_range_v<invalid_range&>, "Invalid iterator types do not form a range");

is_borrowed_range

arene::base::ranges::is_borrowed_range<Rng> is a compile-time query for whether Rng is a range whose iterators's lifetime is not tied to the lifetime of the variable it is obtained from. It is a backport of std::ranges::borrowed_range.

Most commonly this is used to assert in a generic context that it is safe to return an iterator from a function obtained from an input range to that function. For example, the range-algorithm form of find() is only valid for borrowed ranges.

A type Rng is a borrowed range if it satisfies arene::base::ranges::is_range<Rng> and one or both of the following are true:

The variable template arene::base::ranges::is_borrowed_range_v<Rng> provides the same query as arene::base::ranges::is_borrowed_range<Rng>::value in a concise form.

The following examples show use of the trait to determine whether a type is a borrowed range:

static_assert(
ranges::is_borrowed_range_v<owning_range&>,
"A lvalue reference to a range which does not specialize enable_borrowed_range is a borrowed range"
);
static_assert(
!ranges::is_borrowed_range_v<owning_range&&>,
"An rvalue reference to a range which does not specialize enable_borrowed_range is not a borrowed range"
);
static_assert(
ranges::is_borrowed_range_v<user_borrowed_range<int>&>,
"A lvalue reference to a range which specializes enable_borrowed_range is a borrowed range"
);
static_assert(
ranges::is_borrowed_range_v<user_borrowed_range<int>&&>,
"An rvalue reference to a range which specializes enable_borrowed_range is a borrowed range"
);

borrowed_iterator_t

arene::base::ranges::borrowed_iterator_t<Rng> is a type alias that conditionally selects between a ranges iterator or arene::base::ranges::dangling based on whether Rng is a borrowed range. If Rng satisfies arene::base::ranges::is_borrowed_range_v<Rng>, the type alias resolves to arene::base::ranges::iterator_t<Rng>. Otherwise, it resolves to arene::base::ranges::dangling. This is useful for writing generic range algorithms that must always match and return an iterator, even when the input range may not be a borrowed range.