sfl::static_vector
March 16, 2026 ยท View on GitHub
Table of Contents
Summary
Defined in header sfl/static_vector.hpp:
namespace sfl
{
template < typename T, std::size_t N >
class static_vector;
}
sfl::static_vector is a sequence container similar to std::vector, but with a fixed maximum capacity defined at compile time and backed entirely by statically allocated storage. This container internally holds a statically allocated array of size N and stores elements in this array, which avoids dynamic memory allocation and deallocation. This container never uses dynamic memory management. The number of elements in the static vector cannot be greater than N. Attempting to insert more than N elements into this container results in undefined behavior. This design provides a compact and cache-friendly representation optimized for use cases where the maximum size is known in advance. It is also well-suited for bare-metal embedded development where predictable memory usage and no dynamic allocation are critical.
sfl::static_vector is not specialized for bool.
sfl::static_vector meets the requirements of Container, ReversibleContainer, ContiguousContainer and SequenceContainer.
sfl::static_vector can be used in C++20 constant expressions.
Note: Support for C++20 constant expressions is not fully mature in the major compilers (GCC, Clang, MSVC), so the following limitations apply:
- The
data()member function cannot be used in constant expressions. - On GCC, it is not possible to declare
constexprobjects ofstatic_vector. Clang and MSVC allow this, so this is a compiler-specific limitation (possibly a bug).
Template Parameters
-
typename TThe type of the elements.
-
std::size_t NSize of the internal statically allocated array, i.e. the maximal number of elements that this container can contain.
Public Member Types
| Member Type | Definition |
|---|---|
value_type | T |
size_type | Unsigned integer type |
difference_type | Signed integer type |
reference | value_type& |
const_reference | const value_type& |
pointer | Pointer to value_type |
const_pointer | Pointer to const value_type |
iterator | LegacyRandomAccessIterator and LegacyContiguousIterator to value_type |
const_iterator | LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
Public Data Members
static_capacity
static constexpr size_type static_capacity = N;
Public Member Functions
(constructor)
-
static_vector() noexcept;Effects: Constructs an empty container.
Complexity: Constant.
-
static_vector(size_type n);Preconditions:
n <= capacity()Effects: Constructs the container with
nvalue-initialized elements.Complexity: Linear in
n. -
static_vector(size_type n, sfl::default_init_t);Preconditions:
n <= capacity()Effects: Constructs the container with
ndefault-initialized elements.Complexity: Linear in
n. -
static_vector(size_type n, const T& value);Preconditions:
n <= capacity()Effects: Constructs the container with
ncopies of elements with valuevalue.Complexity: Linear in
n. -
template <typename InputIt> static_vector(InputIt first, InputIt last);Preconditions:
std::distance(first, last) <= capacity()Effects: Constructs the container with the contents of the range
[first, last).Note: This overload participates in overload resolution only if
InputItsatisfies requirements of LegacyInputIterator.Complexity: Linear in
std::distance(first, last). -
static_vector(std::initializer_list<T> ilist);Preconditions:
ilist.size() <= capacity()Effects: Constructs the container with the contents of the initializer list
ilist.Complexity: Linear in
ilist.size(). -
static_vector(const static_vector& other);Effects: Copy constructor. Constructs the container with the copy of the contents of
other.Complexity: Linear in size.
-
static_vector(static_vector&& other);Effects: Move constructor. Constructs the container with the contents of
otherusing move semantics.otheris not guaranteed to be empty after the move.otheris in a valid but unspecified state after the move.Complexity: Linear in size.
-
template <typename Range> static_vector(sfl::from_range_t, Range&& range);Preconditions: Number of elements in
rangemust be<= capacity().Effects: Constructs the container with the contents of
range.Note: It is available in C++11. In C++20 are used proper C++20 range concepts.
(destructor)
-
~static_vector();Effects: Destructs the container. The destructors of the elements are called and the used storage is deallocated.
Complexity: Linear in size.
assign
-
void assign(size_type n, const T& value);Preconditions:
n <= capacity()Effects: Replaces the contents of the container with
ncopies of valuevalue.Complexity: Linear in size.
-
template <typename InputIt> void assign(InputIt first, InputIt last);Preconditions:
std::distance(first, last) <= capacity()Effects: Replaces the contents of the container with the contents of the range
[first, last).Note: This overload participates in overload resolution only if
InputItsatisfies requirements of LegacyInputIterator.Note: The behavior is undefined if either
firstorlastis an iterator into*this.Complexity: Linear in size.
-
void assign(std::initializer_list<T> ilist);Preconditions:
ilist.size() <= capacity()Effects: Replaces the contents of the container with the contents of the initializer list
ilist.Complexity: Linear in size.
assign_range
-
template <typename Range> void assign_range(Range&& range);Preconditions: Number of elements in
rangemust be<= capacity().Effects: Replaces the contents of the container with the contents of
range.Note: It is available in C++11. In C++20 are used proper C++20 range concepts.
operator=
-
static_vector& operator=(const static_vector& other);Effects: Copy assignment operator. Replaces the contents with a copy of the contents of
other.Returns:
*this().Complexity: Linear in size.
-
static_vector& operator=(static_vector&& other);Effects: Move assignment operator. Replaces the contents with those of
otherusing move semantics.otheris not guaranteed to be empty after the move.otheris in a valid but unspecified state after the move.Returns:
*this().Complexity: Linear in size.
-
static_vector& operator=(std::initializer_list<T> ilist);Preconditions:
ilist.size() <= capacity()Effects: Replaces the contents with those identified by initializer list
ilist.Returns:
*this().Complexity: Linear in size.
begin, cbegin
-
iterator begin() noexcept; -
const_iterator begin() const noexcept; -
const_iterator cbegin() const noexcept;Effects: Returns an iterator to the first element of the container. If the container is empty, the returned iterator will be equal to
end().Complexity: Constant.
end, cend
-
iterator end() noexcept; -
const_iterator end() const noexcept; -
const_iterator cend() const noexcept;Effects: Returns an iterator to the element following the last element of the container. This element acts as a placeholder; attempting to access it results in undefined behavior.
Complexity: Constant.
rbegin, crbegin
-
reverse_iterator rbegin() noexcept; -
const_reverse_iterator rbegin() const noexcept; -
const_reverse_iterator crbegin() const noexcept;Effects: Returns a reverse iterator to the first element of the reversed container. It corresponds to the last element of the non-reversed container. If the container is empty, the returned iterator is equal to
rend().Complexity: Constant.
rend, crend
-
reverse_iterator rend() noexcept; -
const_reverse_iterator rend() const noexcept; -
const_reverse_iterator crend() const noexcept;Effects: Returns a reverse iterator to the element following the last element of the reversed container. It corresponds to the element preceding the first element of the non-reversed container. This element acts as a placeholder, attempting to access it results in undefined behavior.
Complexity: Constant.
nth
-
iterator nth(size_type pos) noexcept; -
const_iterator nth(size_type pos) const noexcept;Preconditions:
pos <= size()Effects: Returns an iterator to the element at position
pos.If
pos == size(), the returned iterator is equal toend().Complexity: Constant.
index_of
-
size_type index_of(const_iterator pos) const noexcept;Preconditions:
cbegin() <= pos && pos <= cend()Effects: Returns position of the element pointed by iterator
pos, i.e.std::distance(begin(), pos).If
pos == end(), the returned value is equal tosize().Complexity: Constant.
empty
-
bool empty() const noexcept;Effects: Returns
trueif the container has no elements, i.e. whetherbegin() == end().Complexity: Constant.
full
-
bool full() const noexcept;Effects: Returns
trueif the container is full, i.e. whethersize() == capacity().Complexity: Constant.
size
-
size_type size() const noexcept;Effects: Returns the number of elements in the container, i.e.
std::distance(begin(), end()).Complexity: Constant.
max_size
-
static constexpr size_type max_size() const noexcept;Effects: Returns the maximum number of elements the container is able to hold, i.e.
N.Complexity: Constant.
capacity
-
static constexpr size_type capacity() const noexcept;Effects: Returns the maximum number of elements the container is able to hold, i.e.
N.Complexity: Constant.
available
-
size_type available() const noexcept;Effects: Returns the number of elements that can be inserted into the container, i.e.
capacity() - size().Complexity: Constant.
at
-
reference at(size_type pos); -
const_reference at(size_type pos) const;Effects: Returns a reference to the element at specified location
pos, with bounds checking.Complexity: Constant.
Exceptions:
std::out_of_rangeifpos >= size().
operator[]
-
reference operator[](size_type pos) noexcept; -
const_reference operator[](size_type pos) const noexcept;Preconditions:
pos < size()Effects: Returns a reference to the element at specified location pos. No bounds checking is performed.
Note: This operator never inserts a new element into the container.
Complexity: Constant.
front
-
reference front() noexcept; -
const_reference front() const noexcept;Preconditions:
!empty()Effects: Returns a reference to the first element in the container.
Complexity: Constant.
back
-
reference back() noexcept; -
const_reference back() const noexcept;Preconditions:
!empty()Effects: Returns a reference to the last element in the container.
Complexity: Constant.
data
-
T* data() noexcept; -
const T* data() const noexcept;Effects: Returns pointer to the underlying array serving as element storage. The pointer is such that range
[data(), data() + size())is always a valid range, even if the container is empty.data()is not dereferenceable if the container is empty.Complexity: Constant.
clear
-
void clear() noexcept;Effects: Erases all elements from the container. After this call,
size()returns zero.Complexity: Linear in
size().
emplace
-
template <typename... Args> iterator emplace(const_iterator pos, Args&&... args);Preconditions:
!full()cbegin() <= pos && pos <= cend()
Effects: Inserts a new element into the container at position
pos.New element is constructed as
value_type(std::forward<Args>(args)...).args...may directly or indirectly refer to a value in the container.Returns: Iterator to the inserted element.
Complexity: Constant plus linear in
std::distance(pos, end()).
insert
-
iterator insert(const_iterator pos, const T& value);Preconditions:
!full()cbegin() <= pos && pos <= cend()
Effects: Inserts copy of
valueat positionpos.Returns: Iterator to the inserted element.
Complexity: Constant plus linear in
std::distance(pos, end()). -
iterator insert(const_iterator pos, T&& value);Preconditions:
!full()cbegin() <= pos && pos <= cend()
Effects: Inserts
valueusing move semantics at positionpos.Returns: Iterator to the inserted element.
Complexity: Constant plus linear in
std::distance(pos, end()). -
iterator insert(const_iterator pos, size_type n, const T& value);Preconditions:
n <= available()cbegin() <= pos && pos <= cend()
Effects: Inserts
ncopies ofvaluebefore positionpos.Returns: Iterator to the first element inserted, or
posifn == 0.Complexity: Linear in
nplus linear instd::distance(pos, end()). -
template <typename InputIt> iterator insert(const_iterator pos, InputIt first, InputIt last);Preconditions:
std::distance(first, last) <= available()cbegin() <= pos && pos <= cend()
Effects: Inserts elements from the range
[first, last)before positionpos.Note: This overload participates in overload resolution only if
InputItsatisfies requirements of LegacyInputIterator.Returns: Iterator to the first element inserted, or
posiffirst == last.Complexity: Linear in
std::distance(first, last)plus linear instd::distance(pos, end()). -
iterator insert(const_iterator pos, std::initializer_list<T> ilist);Preconditions:
ilist.size() <= available()cbegin() <= pos && pos <= cend()
Effects: Inserts elements from initializer list
ilistbefore positionpos.Returns: Iterator to the first element inserted, or
posifilistis empty.Complexity: Linear in
ilist.size()plus linear instd::distance(pos, end()).
insert_range
-
template <typename Range> iterator insert_range(const_iterator pos, Range&& range);Preconditions: Number of elements in
rangemust be<= available().Effects: Inserts elements from
rangebefore positionpos. Elements are inserted in non-reversing order.rangemust not overlap with the container. Otherwise, the behavior is undefined.Returns: Iterator to the first element inserted, or
posifrangeis empty.Note: It is available in C++11. In C++20 are used proper C++20 range concepts.
emplace_back
-
template <typename... Args> reference emplace_back(Args&&... args);Preconditions:
!full()Effects: Inserts a new element at the end of container.
New element is constructed as
value_type(std::forward<Args>(args)...).Returns: Reference to the inserted element.
Complexity: Constant.
push_back
-
void push_back(const T& value);Preconditions:
!full()Effects: Inserts copy of
valueat the end of container.Complexity: Constant.
-
void push_back(T&& value);Preconditions:
!full()Effects: Inserts
valueusing move semantics at the end of container.Complexity: Constant.
append_range
-
template <typename Range> void append_range(Range&& range);Preconditions: Number of elements in
rangemust be<= available().Effects: Inserts elements from
rangebeforeend(). Elements are inserted in non-reversing order.Note: It is available in C++11. In C++20 are used proper C++20 range concepts.
pop_back
-
void pop_back();Preconditions:
!empty()Effects: Removes the last element of the container.
Complexity: Constant.
erase
-
iterator erase(const_iterator pos);Preconditions:
cbegin() <= pos && pos < cend()Effects: Removes the element at
pos.Returns: Iterator following the last removed element.
If
posrefers to the last element, then theend()iterator is returned. -
iterator erase(const_iterator first, const_iterator last);Preconditions:
cbegin() <= first && first <= last && last <= cend()Effects: Removes the elements in the range
[first, last).Returns: Iterator following the last removed element.
If
last == end()prior to removal, then the updatedend()iterator is returned.If
[first, last)is an empty range, thenlastis returned.
resize
-
void resize(size_type n);Preconditions:
n <= capacity()Effects: Resizes the container to contain
nelements.- If the
n > size(), additional elements are inserted at the end of container. Additional elements are value-initialized. - If the
n < size(), the lastsize() - nelements are removed.
Complexity: Linear in difference between
size()andn. - If the
-
void resize(size_type n, sfl::default_init_t);Preconditions:
n <= capacity()Effects: Resizes the container to contain
nelements.- If the
n > size(), additional elements are inserted at the end of container. Additional elements are default-initialized. - If the
n < size(), the lastsize() - nelements are removed.
Complexity: Linear in difference between
size()andn. - If the
-
void resize(size_type n, const T& value);Preconditions:
n <= capacity()Effects: Resizes the container to contain
nelements.- If the
n > size(), additional copies ofvalueare inserted at the end of container. - If the
n < size(), the lastsize() - nelements are removed.
Complexity: Linear in difference between
size()andn. - If the
swap
-
void swap(static_vector& other);Effects: Exchanges the contents of the container with those of
other.Complexity: Linear in size.
Non-member Functions
operator==
-
template <typename T, std::size_t N> bool operator== ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Checks if the contents of
xandyare equal.The contents of
xandyare equal if the following conditions hold:x.size() == y.size()- Each element in
xcompares equal with the element inyat the same position.
Returns:
trueif the contents of thexandyare equal,falseotherwise.Complexity: Constant if
xandyare of different size, otherwise linear in the size of the container.
operator!=
-
template <typename T, std::size_t N> bool operator!= ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Checks if the contents of
xandyare equal.For details see
operator==.Returns:
trueif the contents of thexandyare not equal,falseotherwise.Complexity: Constant if
xandyare of different size, otherwise linear in the size of the container.
operator<
-
template <typename T, std::size_t N> bool operator< ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Compares the contents of
xandylexicographically. The comparison is performed by a functionstd::lexicographical_compare.Returns:
trueif the contents of thexare lexicographically less than the contents ofy,falseotherwise.Complexity: Linear in the size of the container.
operator>
-
template <typename T, std::size_t N> bool operator> ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Compares the contents of
xandylexicographically. The comparison is performed by a functionstd::lexicographical_compare.Returns:
trueif the contents of thexare lexicographically greater than the contents ofy,falseotherwise.Complexity: Linear in the size of the container.
operator<=
-
template <typename T, std::size_t N> bool operator<= ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Compares the contents of
xandylexicographically. The comparison is performed by a functionstd::lexicographical_compare.Returns:
trueif the contents of thexare lexicographically less than or equal to the contents ofy,falseotherwise.Complexity: Linear in the size of the container.
operator>=
-
template <typename T, std::size_t N> bool operator>= ( const static_vector<T, N>& x, const static_vector<T, N>& y );Effects: Compares the contents of
xandylexicographically. The comparison is performed by a functionstd::lexicographical_compare.Returns:
trueif the contents of thexare lexicographically greater than or equal to the contents ofy,falseotherwise.Complexity: Linear in the size of the container.
swap
-
template <typename T, std::size_t N> void swap ( static_vector<T, N>& x, static_vector<T, N>& y );Effects: Swaps the contents of
xandy. Callsx.swap(y).
erase
-
template <typename T, std::size_t N, typename U> typename static_vector<T, N>::size_type erase(static_vector<T, N>& c, const U& value);Effects: Erases all elements that compare equal to
valuefrom the container.Returns: The number of erased elements.
Complexity: Linear.
erase_if
-
template <typename T, std::size_t N, typename Predicate> typename static_vector<T, N>::size_type erase_if(static_vector<T, N>& c, Predicate pred);Effects: Erases all elements that satisfy the predicate
predfrom the container.predis unary predicate which returnstrueif the element should be removed.Returns: The number of erased elements.
Complexity: Linear.
End of document.