diff --git a/include/const_vector.hpp b/include/const_vector.hpp index 055e626..08c41d3 100644 --- a/include/const_vector.hpp +++ b/include/const_vector.hpp @@ -58,9 +58,13 @@ namespace cc { constexpr ~const_vector() = default; // elements in static array should be destroyed automatically - constexpr const_vector& operator=(const const_vector& other); + constexpr const_vector& operator=(const const_vector& other); // needed to handle *this = *this + template + constexpr const_vector& operator=(const const_vector& other); constexpr const_vector& operator=(const_vector&& other) noexcept; - constexpr const_vector& operator=(const value_type (&array)[N]) noexcept; + //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 void assign(size_type count, const value_type& value) noexcept; template @@ -253,19 +257,33 @@ namespace cc { constexpr const_vector &const_vector::operator=(const const_vector &other) { if (this == &other) return *this; - - static_assert(N == other._len, "Cannot assign const_vector to other with different size"); assign(other.begin(), other.end()); return *this; } + template + template + constexpr const_vector &const_vector::operator=(const const_vector &other) + { + 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) + //if (this == &other) return *this; + + assign(other.begin(), other.end()); + + return *this; + } + template constexpr const_vector &const_vector::operator=(const_vector &&other) noexcept { - static_assert(N == other._len, "Cannot assign const_vector to other with different size"); - if (N != other._len) throw std::exception(); + // cannot occur, otherwise signature would differ + //static_assert(N == other._len, "Cannot assign const_vector to other with different size"); + //if (N != other._len) throw std::exception(); clear(); std::move(other.begin(), other.end(), _arr); @@ -275,7 +293,8 @@ namespace cc { } template - constexpr const_vector &const_vector::operator=(const value_type (&array)[N]) noexcept + template + constexpr const_vector &const_vector::operator=(const value_type (&array)[N2]) noexcept { assign(std::begin(array), std::end(array)); return *this;