template <typename T>
class Span
Defined at line 154 of file ../../third_party/abseil-cpp/absl/types/span.h
A span is conceptually a pointer (ptr) and a length (size) into an already
existing array of contiguous memory; the array it represents references the
elements "ptr[0] .. ptr[size-1]". Passing a properly-constructed `Span`
instead of raw pointers avoids many issues related to index out of bounds
errors.
Spans may also be constructed from containers holding contiguous sequences.
Such containers must supply `data()` and `size() const` methods (e.g
`std::vector
<T
>`, `absl::InlinedVector
<T
, N>`). All implicit conversions to
`absl::Span` from such containers will create spans of type `const T`;
spans which can mutate their values (of type `T`) must use explicit
constructors.
A `Span
<T
>` is somewhat analogous to an `absl::string_view`, but for an array
of elements of type `T`, and unlike an `absl::string_view`, a span can hold a
reference to mutable data. A user of `Span` must ensure that the data being
pointed to outlives the `Span` itself.
You can construct a `Span
<T
>` in several ways:
* Explicitly from a reference to a container type
* Explicitly from a pointer and size
* Implicitly from a container type (but only for spans of type `const T`)
* Using the `MakeSpan()` or `MakeConstSpan()` factory functions.
Examples:
// Construct a Span explicitly from a container:
std::vector
<int
> v = {1, 2, 3, 4, 5};
auto span = absl::Span
<const
int>(v);
// Construct a Span explicitly from a C-style array:
int a[5] = {1, 2, 3, 4, 5};
auto span = absl::Span
<const
int>(a);
// Construct a Span implicitly from a container
void MyRoutine(absl::Span
<const
int> a) {
...
}
std::vector v = {1,2,3,4,5};
MyRoutine(v) // convert to Span
<const
T>
Note that `Span` objects, in addition to requiring that the memory they
point to remains alive, must also ensure that such memory does not get
reallocated. Therefore, to avoid undefined behavior, containers with
associated spans should not invoke operations that may reallocate memory
(such as resizing) or invalidate iterators into the container.
One common use for a `Span` is when passing arguments to a routine that can
accept a variety of array types (e.g. a `std::vector`, `absl::InlinedVector`,
a C-style array, etc.). Instead of creating overloads for each case, you
can simply specify a `Span` as the argument to such a routine.
Example:
void MyRoutine(absl::Span
<const
int> a) {
...
}
std::vector v = {1,2,3,4,5};
MyRoutine(v);
absl::InlinedVector
<int
, 4> my_inline_vector;
MyRoutine(my_inline_vector);
// Explicit constructor from pointer,size
int* my_array = new int[10];
MyRoutine(absl::Span
<const
int>(my_array, 10));
Public Members
static const size_type npos
Public Methods
void Span<T> ()
Defined at line 191 of file ../../third_party/abseil-cpp/absl/types/span.h
void Span<T> (pointer array, size_type length)
Defined at line 192 of file ../../third_party/abseil-cpp/absl/types/span.h
template <size_t N>
void Span<T> (T (&)[N] a)
Implicit conversion constructors
Defined at line 197 of file ../../third_party/abseil-cpp/absl/types/span.h
template <typename V, typename = EnableIfConvertibleFrom<V>, typename = EnableIfValueIsMutable<V>, typename = span_internal::EnableIfNotIsView<V>>
void Span<T> (V & v)
Explicit reference constructor for a mutable `Span
<T
>` type. Can be
replaced with MakeSpan() to infer the type parameter.
Defined at line 205 of file ../../third_party/abseil-cpp/absl/types/span.h
template <typename V, typename = EnableIfConvertibleFrom<V>, typename = EnableIfValueIsConst<V>, typename = span_internal::EnableIfNotIsView<V>>
void Span<T> (const V & v)
Implicit reference constructor for a read-only `Span
<const
T>` type
Defined at line 214 of file ../../third_party/abseil-cpp/absl/types/span.h
template <typename V, typename = EnableIfConvertibleFrom<V>, typename = EnableIfValueIsMutable<V>, span_internal::EnableIfIsView<V> = 0>
void Span<T> (V & v)
Overloads of the above two functions that are only enabled for view types.
This is so we can drop the ABSL_ATTRIBUTE_LIFETIME_BOUND annotation. These
overloads must be made unique by using a different template parameter list
(hence the = 0 for the IsView enabler).
Defined at line 226 of file ../../third_party/abseil-cpp/absl/types/span.h
template <typename V, typename = EnableIfConvertibleFrom<V>, typename = EnableIfValueIsConst<V>, span_internal::EnableIfIsView<V> = 0>
void Span<T> (const V & v)
Defined at line 231 of file ../../third_party/abseil-cpp/absl/types/span.h
template <typename LazyT = T, typename = EnableIfValueIsConst<LazyT>>
void Span<T> (std::initializer_list<value_type> v)
Implicit constructor from an initializer list, making it possible to pass a
brace-enclosed initializer list to a function expecting a `Span`. Such
spans constructed from an initializer list must be of type `Span
<const
T>`.
void Process(absl::Span
<const
int> x);
Process({1, 2, 3});
Note that as always the array referenced by the span must outlive the span.
Since an initializer list constructor acts as if it is fed a temporary
array (cf. C++ standard [dcl.init.list]/5), it's safe to use this
constructor only when the `std::initializer_list` itself outlives the span.
In order to meet this requirement it's sufficient to ensure that neither
the span nor a copy of it is used outside of the expression in which it's
created:
// Assume that this function uses the array directly, not retaining any
// copy of the span or pointer to any of its elements.
void Process(absl::Span
<const
int> ints);
// Okay: the std::initializer_list
<int
> will reference a temporary array
// that isn't destroyed until after the call to Process returns.
Process({ 17, 19 });
// Not okay: the storage used by the std::initializer_list
<int
> is not
// allowed to be referenced after the first line.
absl::Span
<const
int> ints = { 17, 19 };
Process(ints);
// Not okay for the same reason as above: even when the elements of the
// initializer list expression are not temporaries the underlying array
// is, so the initializer list must still outlive the span.
const int foo = 17;
absl::Span
<const
int> ints = { foo };
Process(ints);
Defined at line 271 of file ../../third_party/abseil-cpp/absl/types/span.h
pointer data ()
Span::data()
Returns a pointer to the span's underlying array of data (which is held
outside the span).
Defined at line 281 of file ../../third_party/abseil-cpp/absl/types/span.h
size_type size ()
Span::size()
Returns the size of this span.
Defined at line 286 of file ../../third_party/abseil-cpp/absl/types/span.h
size_type length ()
Span::length()
Returns the length (size) of this span.
Defined at line 291 of file ../../third_party/abseil-cpp/absl/types/span.h
bool empty ()
Span::empty()
Returns a boolean indicating whether or not this span is considered empty.
Defined at line 296 of file ../../third_party/abseil-cpp/absl/types/span.h
reference operator[] (size_type i)
Span::operator[]
Returns a reference to the i'th element of this span.
Defined at line 301 of file ../../third_party/abseil-cpp/absl/types/span.h
reference at (size_type i)
Span::at()
Returns a reference to the i'th element of this span.
Defined at line 308 of file ../../third_party/abseil-cpp/absl/types/span.h
reference front ()
Span::front()
Returns a reference to the first element of this span. The span must not
be empty.
Defined at line 320 of file ../../third_party/abseil-cpp/absl/types/span.h
reference back ()
Span::back()
Returns a reference to the last element of this span. The span must not
be empty.
Defined at line 328 of file ../../third_party/abseil-cpp/absl/types/span.h
iterator begin ()
Span::begin()
Returns an iterator pointing to the first element of this span, or `end()`
if the span is empty.
Defined at line 336 of file ../../third_party/abseil-cpp/absl/types/span.h
const_iterator cbegin ()
Span::cbegin()
Returns a const iterator pointing to the first element of this span, or
`end()` if the span is empty.
Defined at line 342 of file ../../third_party/abseil-cpp/absl/types/span.h
iterator end ()
Span::end()
Returns an iterator pointing just beyond the last element at the
end of this span. This iterator acts as a placeholder; attempting to
access it results in undefined behavior.
Defined at line 349 of file ../../third_party/abseil-cpp/absl/types/span.h
const_iterator cend ()
Span::cend()
Returns a const iterator pointing just beyond the last element at the
end of this span. This iterator acts as a placeholder; attempting to
access it results in undefined behavior.
Defined at line 356 of file ../../third_party/abseil-cpp/absl/types/span.h
reverse_iterator rbegin ()
Span::rbegin()
Returns a reverse iterator pointing to the last element at the end of this
span, or `rend()` if the span is empty.
Defined at line 362 of file ../../third_party/abseil-cpp/absl/types/span.h
const_reverse_iterator crbegin ()
Span::crbegin()
Returns a const reverse iterator pointing to the last element at the end of
this span, or `crend()` if the span is empty.
Defined at line 370 of file ../../third_party/abseil-cpp/absl/types/span.h
reverse_iterator rend ()
Span::rend()
Returns a reverse iterator pointing just before the first element
at the beginning of this span. This pointer acts as a placeholder;
attempting to access its element results in undefined behavior.
Defined at line 377 of file ../../third_party/abseil-cpp/absl/types/span.h
const_reverse_iterator crend ()
Span::crend()
Returns a reverse const iterator pointing just before the first element
at the beginning of this span. This pointer acts as a placeholder;
attempting to access its element results in undefined behavior.
Defined at line 386 of file ../../third_party/abseil-cpp/absl/types/span.h
void remove_prefix (size_type n)
Span::remove_prefix()
Removes the first `n` elements from the span.
Defined at line 393 of file ../../third_party/abseil-cpp/absl/types/span.h
void remove_suffix (size_type n)
Span::remove_suffix()
Removes the last `n` elements from the span.
Defined at line 402 of file ../../third_party/abseil-cpp/absl/types/span.h
Span<T> subspan (size_type pos, size_type len)
Span::subspan()
Returns a `Span` starting at element `pos` and of length `len`. Both `pos`
and `len` are of type `size_type` and thus non-negative. Parameter `pos`
must be
<
= size(). Any `len` value that points past the end of the span
will be trimmed to at most size() - `pos`. A default `len` value of `npos`
ensures the returned subspan continues until the end of the span.
Examples:
std::vector
<int
> vec = {10, 11, 12, 13};
absl::MakeSpan(vec).subspan(1, 2); // {11, 12}
absl::MakeSpan(vec).subspan(2, 8); // {12, 13}
absl::MakeSpan(vec).subspan(1); // {11, 12, 13}
absl::MakeSpan(vec).subspan(4); // {}
absl::MakeSpan(vec).subspan(5); // throws std::out_of_range
Defined at line 423 of file ../../third_party/abseil-cpp/absl/types/span.h
Span<T> first (size_type len)
Span::first()
Returns a `Span` containing first `len` elements. Parameter `len` is of
type `size_type` and thus non-negative. `len` value must be
<
= size().
Examples:
std::vector
<int
> vec = {10, 11, 12, 13};
absl::MakeSpan(vec).first(1); // {10}
absl::MakeSpan(vec).first(3); // {10, 11, 12}
absl::MakeSpan(vec).first(5); // throws std::out_of_range
Defined at line 440 of file ../../third_party/abseil-cpp/absl/types/span.h
Span<T> last (size_type len)
Span::last()
Returns a `Span` containing last `len` elements. Parameter `len` is of
type `size_type` and thus non-negative. `len` value must be
<
= size().
Examples:
std::vector
<int
> vec = {10, 11, 12, 13};
absl::MakeSpan(vec).last(1); // {13}
absl::MakeSpan(vec).last(3); // {11, 12, 13}
absl::MakeSpan(vec).last(5); // throws std::out_of_range
Defined at line 457 of file ../../third_party/abseil-cpp/absl/types/span.h
Friends
template <typename H>
H Span (H hSpan<T> v)