From f08c2890f1015c867804aab8e3df325ded8e951c Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 22 Jul 2024 23:30:24 +0200 Subject: [PATCH] updated wrong noexcepts and throw statements --- include/const_vector.hpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/const_vector.hpp b/include/const_vector.hpp index b089aa4..c2b9134 100644 --- a/include/const_vector.hpp +++ b/include/const_vector.hpp @@ -48,7 +48,7 @@ namespace cc { template constexpr explicit const_vector(const value_type (&array)[N2]) noexcept; - constexpr const_vector(std::initializer_list values) noexcept; + constexpr const_vector(std::initializer_list values); template constexpr const_vector(InputIt first, InputIt last); @@ -62,7 +62,7 @@ namespace cc { constexpr ~const_vector() = default; // elements in static array should be destroyed automatically - constexpr const_vector& operator=(const const_vector& other); // needed to handle *this = *this + constexpr const_vector& operator=(const const_vector& other) noexcept; // needed to handle *this = *this template constexpr const_vector& operator=(const const_vector& other); constexpr const_vector& operator=(const_vector&& other) noexcept; @@ -70,8 +70,8 @@ namespace cc { constexpr const_vector& operator=(const_vector&& other); //constexpr const_vector& operator=(const value_type (&array)[N]) noexcept; // not needed as functionally equivalent to templated overload template - constexpr const_vector& operator=(const value_type (&array)[N2]) noexcept; - constexpr const_vector& operator=(std::initializer_list values) noexcept; + constexpr const_vector& operator=(const value_type (&array)[N2]); + constexpr const_vector& operator=(std::initializer_list values); constexpr void assign(size_type count, const value_type& value) noexcept; template @@ -217,9 +217,10 @@ namespace cc { } template - constexpr const_vector::const_vector(std::initializer_list values) noexcept + constexpr const_vector::const_vector(std::initializer_list values) : _size(values.size()) { + if (values.size() > N) throw std::invalid_argument("Initializer list in assign has more elements than size" + std::to_string(values.size()) + ">=" + std::to_string(N)); std::move(values.begin(), values.end(), _arr); } @@ -266,10 +267,13 @@ namespace cc { } template - constexpr const_vector &const_vector::operator=(const const_vector &other) + constexpr const_vector &const_vector::operator=(const const_vector &other) noexcept { if (this == &other) return *this; + // event though assign may throw an exception noexcept for this operator should be fine, + // as std::distance(other.begin(), other.end()) cannot be greater than the capacity, + // because the signatures would have to differ assign(other.begin(), other.end()); return *this; @@ -282,7 +286,7 @@ namespace cc { if (other.size() > _len) throw std::invalid_argument("size of other has to be equal to or smaller than this"); // this is not necessary, as it should call the non templated operator= - // (and it throws an error without a cast) + // (and throws an error without a cast) //if (this == &other) return *this; assign(other.begin(), other.end()); @@ -319,14 +323,14 @@ namespace cc { template template - constexpr const_vector &const_vector::operator=(const value_type (&array)[N2]) noexcept + constexpr const_vector &const_vector::operator=(const value_type (&array)[N2]) { assign(std::begin(array), std::end(array)); return *this; } template - constexpr const_vector &const_vector::operator=(std::initializer_list values) noexcept + constexpr const_vector &const_vector::operator=(std::initializer_list values) { assign(std::move(values)); return *this; @@ -346,7 +350,7 @@ namespace cc { constexpr void const_vector::assign(InputIt first, InputIt last) { auto distance = std::distance(first, last); - if (distance > N) throw std::invalid_argument("Iterator distance in assign surpasses size" + std::to_string(distance) + ">=" + std::to_string(N)); + if (distance > N) throw std::invalid_argument("Iterator distance in assign surpasses size " + std::to_string(distance) + " >= " + std::to_string(N)); _size = distance; std::copy(first, last, begin()); }