跳转至

cf::span

A non-owning view into a contiguous sequence of elements. More...

#include <span.h>

Public Functions

Name
constexpr span()
Default constructor. Creates an empty span.
constexpr span(T * data, size_t size)
Constructs a span from a pointer and size.
template <size_t N>
constexpr
span(T(&) arr[N])
Constructs a span from a C array.
constexpr span(std::vector< T > & vec)
Constructs a span from a std::vector.
template <typenameU =T,std::enable_if_t< std::is_const< U >::value, int > =0>
constexpr
span(const std::vector< std::remove_const_t< T > > & vec)
Constructs a const span from a const std::vector.
template <size_t N>
constexpr
span(std::array< T, N > & arr)
Constructs a span from a std::array.
template <size_t N,typenameU =T,std::enable_if_t< std::is_const< U >::value, int > =0>
constexpr
span(const std::array< std::remove_const_t< T >, N > & arr)
Constructs a const span from a const std::array.
constexpr span(constspan & other) =default
Default copy constructor.
constexprspan & operator=(constspan & other) =default
Default copy assignment operator.
constexprT * data() const
Returns pointer to the first element.
constexprsize_t size() const
Returns the number of elements in the span.
constexprbool empty() const
Checks if the span is empty.
constexprT & operator[](size_t index) const
Accesses element at specified index.
constexprT & front() const
Returns the first element.
constexprT & back() const
Returns the last element.
constexprT * begin() const
Returns iterator to the first element.
constexprT * end() const
Returns iterator to one past the last element.
constexprspan< T > first(size_t count) const
Creates a span of the first count elements.
constexprspan< T > last(size_t count) const
Creates a span of the last count elements.
constexprspan< T > subspan(size_t offset, size_t count =static_cast< size_t >(-1)) const
Creates a subspan starting at offset.

Detailed Description

template <typenameT >
class cf::span;

A non-owning view into a contiguous sequence of elements.

Template Parameters:

  • T Element type. Must be trivially copyable.

Provides a lightweight, bounds-checked (when using methods) interface to access elements without owning the underlying data. Similar to std::span from C++20 but implemented for C++17 compatibility.

std::vector<int> vec = {1, 2, 3, 4, 5};
cf::span<int> s = vec;
int first = s[0];  // Access first element

Public Functions Documentation

function span

inline constexpr span()

Default constructor. Creates an empty span.

function span

inline constexpr span(
    T * data,
    size_t size
)

Constructs a span from a pointer and size.

Parameters:

  • data Pointer to the first element. Must be non-null if size > 0.
  • size Number of elements in the sequence.

function span

template <size_t N>
inline constexpr span(
    T(&) arr[N]
)

Constructs a span from a C array.

Parameters:

  • arr C array to view.

Template Parameters:

  • N Array size deduced from the input.

function span

inline constexpr span(
    std::vector< T > & vec
)

Constructs a span from a std::vector.

Parameters:

  • vec Vector to view. Does not take ownership.

function span

template <typenameU  =T,
std::enable_if_t< std::is_const< U >::value, int >  =0>
inline constexpr span(
    const std::vector< std::remove_const_t< T > > & vec
)

Constructs a const span from a const std::vector.

Parameters:

  • vec Const vector to view. Does not take ownership.

Template Parameters:

  • U Template parameter to enable only for const T.

function span

template <size_t N>
inline constexpr span(
    std::array< T, N > & arr
)

Constructs a span from a std::array.

Parameters:

  • arr Array to view.

Template Parameters:

  • N Array size.

function span

template <size_t N,
typenameU  =T,
std::enable_if_t< std::is_const< U >::value, int >  =0>
inline constexpr span(
    const std::array< std::remove_const_t< T >, N > & arr
)

Constructs a const span from a const std::array.

Parameters:

  • arr Const array to view.

Template Parameters:

  • N Array size.
  • U Template parameter to enable only for const T.

function span

constexpr span(
    constspan & other
) =default

Default copy constructor.

function operator=

constexprspan & operator=(
    constspan & other
) =default

Default copy assignment operator.

function data

inline constexprT * data() const

Returns pointer to the first element.

Return: Pointer to the underlying data, or nullptr if empty.

function size

inline constexprsize_t size() const

Returns the number of elements in the span.

Return: Number of elements in the sequence.

function empty

inline constexprbool empty() const

Checks if the span is empty.

Return: true if size() == 0, false otherwise.

function operator[]

inline constexprT & operator[](
    size_t index
) const

Accesses element at specified index.

Parameters:

  • index Zero-based element index.

Return: Reference to the element at the specified index.

Warning: No bounds checking is performed. Caller must ensure index < size().

function front

inline constexprT & front() const

Returns the first element.

Exceptions:

  • None.

Return: Reference to the first element.

Since: 0.1

Note: None.

Warning: Undefined behavior if the span is empty.

function back

inline constexprT & back() const

Returns the last element.

Exceptions:

  • None.

Return: Reference to the last element.

Since: 0.1

Note: None.

Warning: Undefined behavior if the span is empty.

function begin

inline constexprT * begin() const

Returns iterator to the first element.

Exceptions:

  • None.

Return: Pointer to the first element (begin iterator).

Since: 0.1

Note: None.

function end

inline constexprT * end() const

Returns iterator to one past the last element.

Exceptions:

  • None.

Return: Pointer to one past the last element (end iterator).

Since: 0.1

Note: None.

function first

inline constexprspan< T > first(
    size_t count
) const

Creates a span of the first count elements.

Parameters:

  • count Number of elements to include.

Exceptions:

  • None.

Return: New span containing the first count elements.

Since: 0.1

Note: None.

Warning: Undefined behavior if count > size().

function last

inline constexprspan< T > last(
    size_t count
) const

Creates a span of the last count elements.

Parameters:

  • count Number of elements to include.

Exceptions:

  • None.

Return: New span containing the last count elements.

Since: 0.1

Note: None.

Warning: Undefined behavior if count > size().

function subspan

inline constexprspan< T > subspan(
    size_t offset,
    size_t count =static_cast< size_t >(-1)
) const

Creates a subspan starting at offset.

Parameters:

  • offset Starting element index.
  • count Number of elements to include. If -1 (default), includes all elements from offset to end.

Exceptions:

  • None.

Return: New span containing the specified elements.

Since: 0.1

Note: None.

Warning: Undefined behavior if offset + count > size().


Updated on 2026-03-09 at 10:14:00 +0000