12#include <initializer_list>
28template <
typename T, std::
size_t Capacity>
45 if (init.size() > Capacity) {
46 throw std::out_of_range(
"StaticVector initializer list too large");
48 for (
const auto& value : init) {
58 for (
size_type i = 0; i < other.m_size; ++i) {
69 for (
size_type i = 0; i < other.m_size; ++i) {
83 for (
size_type i = 0; i < other.m_size; ++i) {
96 std::is_nothrow_move_constructible_v<T> && std::is_nothrow_destructible_v<T>) {
99 for (
size_type i = 0; i < other.m_size; ++i) {
117 bool empty() const noexcept {
return m_size == 0; }
146 if (index >= m_size) {
147 throw std::out_of_range(
"StaticVector index out of range");
149 return data()[index];
154 if (index >= m_size) {
155 throw std::out_of_range(
"StaticVector index out of range");
157 return data()[index];
168 const T&
back() const noexcept {
return data()[m_size - 1]; }
171 T*
data() noexcept {
return std::launder(
reinterpret_cast<T*
>(m_storage)); }
173 const T*
data() const noexcept {
return std::launder(
reinterpret_cast<const T*
>(m_storage)); }
195 if (m_size >= Capacity) {
196 throw std::out_of_range(
"StaticVector capacity exceeded");
198 new (
data() + m_size) T(value);
208 if (m_size >= Capacity) {
209 throw std::out_of_range(
"StaticVector capacity exceeded");
211 new (
data() + m_size) T(std::move(value));
222 template <
typename... Args>
224 if (m_size >= Capacity) {
225 throw std::out_of_range(
"StaticVector capacity exceeded");
227 T* slot =
new (
data() + m_size) T(std::forward<Args>(args)...);
253 if (new_size > Capacity) {
254 throw std::out_of_range(
"StaticVector resize exceeds capacity");
256 while (m_size > new_size) {
259 while (m_size < new_size) {
271 if (new_size > Capacity) {
272 throw std::out_of_range(
"StaticVector resize exceeds capacity");
274 while (m_size > new_size) {
277 while (m_size < new_size) {
292 if (requested > Capacity) {
293 throw std::out_of_range(
"StaticVector reserve exceeds capacity");
299 alignas(T)
unsigned char m_storage[
sizeof(T) * Capacity]{};
Fixed-capacity vector with no heap allocation.
StaticVector(const StaticVector &other)
Copy constructor - deep copies all elements.
const T & operator[](size_type index) const noexcept
Access element by index (no bounds checking).
const T & front() const noexcept
Access first element (undefined if empty).
void reserve(size_type requested)
Reserve capacity (no-op for StaticVector).
T & back() noexcept
Access last element (undefined if empty).
T & front() noexcept
Access first element (undefined if empty).
StaticVector() noexcept=default
Default constructor - creates empty vector.
void push_back(T &&value)
Add element by move.
void resize(size_type new_size)
Resize the vector.
T & operator[](size_type index) noexcept
Access element by index (no bounds checking).
void pop_back() noexcept
Remove last element.
T value_type
Element type.
iterator begin() noexcept
const T & back() const noexcept
Access last element (undefined if empty).
void push_back(const T &value)
Add element by copy.
const_iterator end() const noexcept
void resize(size_type new_size, const T &value)
Resize with fill value.
T & emplace_back(Args &&... args)
Construct element in-place at end.
T & at(size_type index)
Access element with bounds checking.
T * iterator
Mutable iterator type.
size_type size() const noexcept
Get current number of elements.
const_iterator cend() const noexcept
bool empty() const noexcept
Check if vector is empty.
const T * data() const noexcept
Get pointer to underlying storage.
StaticVector & operator=(const StaticVector &other)
Copy assignment operator.
StaticVector(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move constructor - moves all elements.
const_iterator begin() const noexcept
constexpr size_type capacity() const noexcept
Get maximum capacity (compile-time constant).
void clear() noexcept
Remove all elements.
StaticVector & operator=(StaticVector &&other) noexcept(std::is_nothrow_move_constructible_v< T > &&std::is_nothrow_destructible_v< T >)
Move assignment operator.
const T * const_iterator
Const iterator type.
T * data() noexcept
Get pointer to underlying storage.
std::size_t size_type
Size/index type.
const T & at(size_type index) const
Access element with bounds checking.
const_iterator cbegin() const noexcept
~StaticVector()
Destructor - properly destroys all elements.