![]() |
Arene Base
Fundamental Utilities For Safety Critical C++
|
The ranges/type_traits subpackage provides C++14-compatible type traits for properties described by concepts in std::ranges.
The public export header is:
enable_borrowed_rangearene::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:
Only the range whose iterators do not depend on the lifetime of the range object is opted in:
The class-trait and variable-template forms can then be queried at compile time:
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_rangearene::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:
Such a range is opted out so that its non-constant-time size() is disregarded:
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:
is_rangearene::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:
arene::base::ranges::begin(std::declval<Rng>()) is well-formed.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:
is_borrowed_rangearene::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:
arene::base::ranges::enable_borrowed_range<arene::base::remove_cvref_t<Rng>> is specialized to true for the type.Rng is an lvalue reference.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:
borrowed_iterator_tarene::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.