etl/docs/containers/vectors/indirect-vector.md
rleigh-electradx 25e2d6e8a3
Adopt the Library Fundamentals TS make_array design for the sequence factories (#1553)
* Adopt the Library Fundamentals TS make_array design for the sequence factories

make_array, make_deque, make_vector, make_list, make_forward_list and
make_indirect_vector now follow the Library Fundamentals TS
std::experimental::make_array design: a defaulted leading template
parameter selects the element type explicitly, falling back to the
decayed common type of the arguments when it is not supplied. A shared
trait (etl::private_make::element_type) performs the selection lazily,
so common_type is not instantiated when the element type is explicit.

This fixes the explicit-element-type calling style for the four
common-type factories, where make_vector<char>(lvalue) never compiled:
the explicit argument pinned the first pack parameter to char, making
it a char&& that no lvalue can bind to. It also gives make_array and
make_deque the deduced form (e.g. make_array(1, 2L) is array<long, 2>)
for free. All arguments are converted with static_cast, as in the
make_array const-lvalue fix, so const-qualified lvalues and narrowing
initialisers work in both forms.

Intentional behaviour change: an explicit element type now converts
every argument instead of pinning only the first. make_vector<char>(0,
1, ...) previously produced vector<int> - the explicit char affected
only the first argument and the element type was still the common type
- and now produces vector<char>. test_make_vector documented the old
behaviour and has been updated.

The keyed set and map factories are unchanged: their key/mapped types
are separate mandatory template parameters ahead of a fully-deduced
pack, so the explicit-type style already works, and retrofitting
deduction there would interact with the defaulted comparator/hash
parameters.

Adds explicit-element-type tests for the four common-type factories
(const/non-const lvalues plus narrowing initialisers) and
deduced-element-type tests for make_array and make_deque, and updates
the four documented signatures.

* Address review comments on the make_xxx factory design

- Forward same-type arguments in the make_xxx factories instead of
  casting them. static_cast produced a prvalue that, before C++17, had
  to be materialised through the move constructor, rejecting
  copyable-but-non-movable element types. The new
  private_make::convert helper forwards arguments that already have
  the element type and static_casts the rest.
- Drop ETL_NOEXCEPT from make_array: conversions can throw, and the
  other factories do not declare it.
- Remove the redundant etl::decay in element_type<void, ...>;
  etl::common_type already decays its arguments.
- Stop leaking the etl::private_make implementation namespace into the
  documented factory signatures.

* Add a test for make_array with an explicit element type and no arguments

etl::make_array<int>() produces an empty etl::array<int, 0> and now has
a regression test. A call with neither an element type nor arguments
remains ill-formed, matching the Library Fundamentals TS: there is
nothing to deduce the element type from. The array documentation now
states this explicitly.

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
2026-08-24 08:53:25 +01:00

16 KiB

title weight
indirect_vector 3

{{< callout type="info">}} Header: indirect_vector.h
Similar to: std::vector {{< /callout >}}

A fixed capacity vector that uses indirection to access the elements.
Can be more efficient for vectors of large objects, especially when inserting, erasing or sorting.
Supplies function adaptors to allow efficient application of external algorithms.

etl::indirect_vector<typename T, size_t SIZE>
etl::indirect_vector_ext<typename T>

Inherits from etl::iindirect_vector<T>.
etl::iindirect_vector may be used as a size independent pointer or reference type for any etl::indirect_vector instance.

External buffer

etl::indirect_vector_ext<typename T>

With this template, the constructor expects pointer and size parameters to the externally provided indirection lookup vector and pool.

Template deduction guides

C++17 and above

template <typename... T>
etl::indirect_vector(T...)

Example

etl::indirect_vector data{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Defines data as an indirect_vector of int, of length 10, containing the supplied data.

Make template

C++11 and above

template <typename T = void, typename... TValues>
constexpr auto make_indirect_vector(TValues&&... values)
  -> etl::indirect_vector</*element-type*/, sizeof...(TValues)>

The element type is T when supplied, otherwise the decayed common type of the arguments (the Library Fundamentals TS make_array design).

Example

// Deduced element type: etl::indirect_vector<int, 10>.
auto data = etl::make_indirect_vector(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

// Explicit element type: etl::indirect_vector<char, 10>. Each argument is converted.
auto chars = etl::make_indirect_vector<char>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

Member types

value_type              T
size_type               std::size_t
difference_type         std::ptrdiff_t
reference               value_type&
const_reference         const value_type&
pointer                 value_type*
const_pointer           const value_type*
indirect_iterator       Iterator to the internal lookup vector
indirect_const_iterator Constant iterator to the internal lookup vector
const_pointer           const value_type*
iterator                Random access iterator
const_iterator          Constant random access iterator
reverse_iterator        std::reverse_iterator<iterator>
const_reverse_iterator  std::reverse_iterator<const_iterator>

Iterators

Both iterator and const_iterator define indirection() member functions that return an indirect_iterator or indirect_const_iterator to the internal lookup vector.

Access to these iterators allows an external algorithm to manipulate the indirect_vector without moving the objects themselves, thereby vastly increasing the efficiency for vectors of large objects.

Function Object Adaptors

Unary and binary function object wrappers are defined to allow algorithms to manipulate the contents of an indirect_vector using a standard function object with indirect iterators.

template <typename TUnaryFunction, typename TReturnType = void>
class unary_function_adaptor

template <typename TBinaryFunction, typename TReturnType = void>
class binary_function_adaptor

Example

Sorting without moving or copying large objects.

// The large data that we are storing in the vector.
struct Data
{
  std::array<int, 1000> values;
};

// How to compare Data objects.
bool operator <(const Data& lhs, const Data& rhs)
{
  return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

// The vector of data.
using Vector = etl::indirect_vector<Data, 10>;
Vector v;

// std::less wrapped in a binary_function_adaptor.
using LessThan = typename Vector::binary_function_adaptor<std::less<Data>, bool>;

// Sort the vector without moving or copying actual Data objects.
std::sort(v.begin().indirection(),
          v.end().indirection(),
          LessThan());

{{< callout type="warning">}} Warning: This technique is not suitable for algorithms that may leave the container with multiple copies of an object, such as std::remove_if. This is because the algorithm may create multiple copies of the indirect iterator, but only one copy of the object that they point to. {{< /callout >}}

Constructor

Internal buffer

etl::indirect_vector<typename T, const size_t SIZE>()
etl::indirect_vector<typename T, const size_t SIZE>(size_t initialSize)
etl::indirect_vector<typename T, const size_t SIZE>(size_t initialSize, const T& value)

template <typename TIterator>
etl::indirect_vector<typename T, const size_t SIZE>(TIterator begin, TIterator end)

etl::indirect_vector<typename T, const size_t SIZE>(const etl::indirect_vector<typename T, const size_t SIZE>&)
etl::indirect_vector<typename T, const size_t SIZE>(etl::indirect_vector<typename T, const size_t SIZE>&&)

Description
If the vector is full then emits an etl::vector_full.
If asserts or exceptions are not enabled then undefined behaviour occurs.

External buffer

etl::indirect_vector<typename T, const size_t SIZE>(etl::ivector<T*>& lookup, etl::ipool& storage)
etl::indirect_vector<typename T, const size_t SIZE>(size_t initialSize, etl::ivector<T*>& lookup, etl::ipool& storage)
etl::indirect_vector<typename T, const size_t SIZE>(size_t initialSize, const T& value, etl::ivector<T*>& lookup, etl::ipool& storage)

template <typename TIterator>
etl::indirect_vector<typename T, const size_t SIZE>(TIterator begin, TIterator end, etl::ivector<T*>& lookup, etl::ipool& storage)

etl::indirect_vector<typename T, const size_t SIZE>(const etl::indirect_vector<typename T, const size_t SIZE>&, etl::ivector<T*>& lookup, etl::ipool& storage)

etl::indirect_vector<typename T, const size_t SIZE>(etl::indirect_vector<typename T, const size_t SIZE>&&, etl::ivector<T*>& lookup, etl::ipool& storage)

Description
If the vector is full then emits an etl::vector_full.
If asserts or exceptions are not enabled then undefined behaviour occurs.

Element access

reference at(size_t i)
const_reference at(size_t i) const

Description
Returns a reference or const reference to the indexed element.
Emits an etl::vector_out_of_range if the index is out of range of the array.
If asserts or exceptions are not enabled then undefined behaviour occurs.


reference operator[](size_t i)
const_reference operator[](size_t i) const

Description
Returns a reference or const reference to the indexed element. if the index is out of range of the array then undefined behaviour occurs.


reference front()
const_reference front() const

Description
Returns a reference or const reference to the first element. Undefined behaviour if the vector is empty.


reference back()
const_reference back() const

Description
Returns a reference or const reference to the last element. Undefined behaviour if the vector is empty.

Iterators

iterator begin()
const_iterator begin() const
const_iterator cbegin() const

Description
Returns an iterator to the beginning of the vector.


iterator end()
const_iterator end() const
const_iterator cend() const

Description
Returns an iterator to the end of the vector.


iterator rbegin()
const_reverse_iterator rbegin() const
const_reverse_iterator crbegin() const

Description
Returns a reverse iterator to the beginning of the vector.


iterator rend()
const_reverse_iterator rend() const
const_reverse_iterator crend() const

Description
Returns a reverse iterator to the end of the vector.

Capacity

bool empty() const

Description
Returns true if the size of the vector is zero, otherwise false.


bool full() const

Description
Returns true if the size of the vector is SIZE, otherwise false.


size_t size() const

Description
Returns the size of the vector.


void resize(size_t new_size, T value = T())

Description
Resizes the vector, up to the maximum capacity. Emits an etl::vector_full if the vector does not have the capacity.


size_t max_size() const

Description
Returns the maximum possible size of the vector.


size_t capacity() const

Description
Returns the maximum possible size of the vector.


size_t available() const

Description
Returns the remaining available capacity in the vector.

Modifiers

template <typename TIterator>
void assign(TIterator begin, TIterator end)

void assign(size_t n, const T& value)

Description
Fills the vector with the values. Emits etl::vector_iterator if the distance between begin and end is illegal. (debug mode only). If asserts or exceptions are not enabled undefined behaviour occurs.


void push_back(const T& value)
void push_back(T&& value)

Description
Pushes a value to the back of the vector. If the vector is full then emits an etl::vector_full. If asserts or exceptions are not enabled undefined behaviour occurs.


C++03

void emplace() 
void emplace(const T1& value1)
void emplace(const T1& value1, const T2& value2)
void emplace(const T1& value1, const T2& value2, const T3& value3)
void emplace(const T1& value1, const T2& value2, const T3& value3, const T4& value4)

Description
Constructs an item at the back of the the vector 'in place'.
Supports up to four constructor parameters.


C++11

template <typename… Args>
void emplace(Args&&… args)

Description
Constructs an item at the back of the the vector 'in place'. Pushes a value to the back of the vector.
If the vector is full then emits an etl::vector_full.
If asserts or exceptions are not enabled undefined behaviour occurs.


C++03

void emplace_back(const T1& value1)
void emplace_back(const T1& value1, const T2& value2)
void emplace_back(const T1& value1, const T2& value2, const T3& value3)
void emplace_back(const T1& value1, const T2& value2, const T3& value3, const T4& value4)

Description
Constructs an item at the back of the the vector 'in place'.
If the vector is full then emits an etl::vector_full. If asserts or exceptions are not enabled undefined behaviour occurs.
Supports up to four constructor parameters.
Before: 20.35.10


reference emplace_back() 20.38.0
reference emplace_back(const T1& value1)
reference emplace_back(const T1& value1, const T2& value2)
reference emplace_back(const T1& value1, const T2& value2, const T3& value3)
reference emplace_back(const T1& value1, const T2& value2, const T3& value3, const T4& value4)

Description
Constructs an item at the back of the the vector 'in place'.
If the vector is full then emits an etl::vector_full. If asserts or exceptions are not enabled undefined behaviour occurs.
Supports up to four constructor parameters.
Since: 20.35.10


C++11

template <typename Args...>
void emplace_back(Args&&... args)

Description
Constructs an item at the back of the the vector 'in place'.
If the vector is full then emits an etl::vector_full. If asserts or exceptions are not enabled undefined behaviour occurs.
Before: 20.35.10


template <typename Args...>
reference emplace_back(Args&&... args)

Description
Constructs an item at the back of the the vector 'in place'.
Supports up to four constructor parameters.
If the vector is full then emits an etl::vector_full. If asserts or exceptions are not enabled undefined behaviour occurs.
Since: 20.35.10


void pop_back()

Description
Pop a value from the back of the vector. If the vector is empty and ETL_CHECK_PUSH_POP is defined then emits an etl::vector_empty.
If asserts or exceptions are not enabled undefined behaviour occurs.


template <typename TIterator>
void insert(iterator position, TIterator begin, TIterator end)

Description
Insert the range [begin, end) at position.
Before: 20.19.0


template <typename TIterator>
iterator insert(const_iterator position, TIterator begin, TIterator end)

Description
Insert the range [begin, end) at position.
Since: 20.20.0

Return position


iterator insert(iterator position, const T& value)
iterator insert(iterator position, T&& value)

Description
Insert value at position.

Return position


iterator insert(const_iterator position, const T& value)
iterator insert(const_iterator position, T&& value)

Description
Insert value at position.

Return position


void insert(iterator position, size_t n, const T& value)

Description
Inserts n copies of value at position.


iterator insert(const_iterator position, size_t n, const T& value)

Description
Inserts values in to the vector. If the vector is full then emits an etl::vector_full exception. If asserts or exceptions are not enabled undefined behaviour occurs.


template <typename TIterator>
iterator erase(TIterator begin, TIterator end)

Description
Erase elements in the range [begin, end).

Return
An iterator to the element after the erased range.


<=20.19.0

iterator erase(iterator position)

Description
Erases values in the vector.
Iterators are not checked.
Before: 20.20.0


iterator erase(iterator position)
iterator erase(const_iterator position)

Description
Erases values in the vector.
Iterators are not checked.
Since: 20.20.0

Return
An iterator to the element after the erased element.


void clear()

Description
Clears the vector to a size of zero.


void sort()

Description
Sorts the elements using the 'less than' operator.


template <typename TCompare>
void sort(TCompare compare)

Description
Sorts the elements using the supplied compare.


void sort(iterator first, iterator last)

Description
Sorts a range of elements using the supplied 'less than' operator.


template <typename TCompare>
void sort(iterator first, iterator last, TCompare compare)

Description
Sorts a range of elements using the supplied compare.


void stable_sort()

Description
Stable sorts the elements using the 'less than' operator.


template <typename TCompare>
void stable_sort(TCompare compare)

Description
Stable sorts the elements using the supplied compare.


void stable_sort(iterator first, iterator last)

Description
Stable sorts a range of elements using the supplied 'less than' operator.


template <typename TCompare>
void stable_sort(iterator first, iterator last, TCompare compare)

Description
Stable sorts a range of elements using the supplied compare.

Non-member functions

operator ==  

Description
true if the contents of the vectors are equal, otherwise false.


operator !=  

Description
true if the contents of the vectors are not equal, otherwise false.


operator <

Description
true if the contents of the lhs are lexicographically less than the contents of the rhs, otherwise false.


operator <=

Description
true if the contents of the lhs are lexicographically less than or equal to the contents of the rhs, otherwise false.


operator >

Description
true if the contents of the lhs are lexicographically greater than the contents of the rhs, otherwise false.


operator >=

Description
true if the contents of the lhs are lexicographically greater than or equal to the contents of the rhs, otherwise false.