From 6ebec36b2de9efb1f1d6da900c8e6b9b6911d54f Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 29 Jul 2024 16:53:38 +0200 Subject: [PATCH] fixed emplace() --- include/const_vector.hpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/include/const_vector.hpp b/include/const_vector.hpp index 3fecbcb..94b6f90 100644 --- a/include/const_vector.hpp +++ b/include/const_vector.hpp @@ -467,19 +467,20 @@ namespace cc { } template - template + template constexpr const_vector::iterator const_vector::emplace(const_vector::const_iterator pos, Args &&... args) { - if (_size == N) throw std::exception(); + if (_size == N) throw std::length_error("No space left in vector"); - T obj(std::forward(args)...); - - ptrdiff_t i = pos - _arr; - std::move(_arr + i, _arr + _size, _arr + i + 1); - _erase_no_move(pos); - _arr[i] = std::move(obj); - - return pos; + auto it = const_cast(pos); + + if (it != end()) { + std::move_backward(it, end(), end() + 1); + } + *it = T(std::forward(args)...); + + ++_size; + return it; } template