added overloads to and bugfixed operator=

This commit is contained in:
Patrick 2024-01-06 23:37:44 +01:00
parent a89e03fb94
commit 8bbddf74fe
1 changed files with 26 additions and 7 deletions

View File

@ -58,9 +58,13 @@ namespace cc {
constexpr ~const_vector() = default; // elements in static array should be destroyed automatically constexpr ~const_vector() = default; // elements in static array should be destroyed automatically
constexpr const_vector<T, N>& operator=(const const_vector& other); constexpr const_vector<T, N>& operator=(const const_vector& other); // needed to handle *this = *this
template <std::size_t N2>
constexpr const_vector<T, N>& operator=(const const_vector<value_type, N2>& other);
constexpr const_vector<T, N>& operator=(const_vector&& other) noexcept; constexpr const_vector<T, N>& operator=(const_vector&& other) noexcept;
constexpr const_vector<T, N>& operator=(const value_type (&array)[N]) noexcept; //constexpr const_vector<T, N>& operator=(const value_type (&array)[N]) noexcept; // not needed as functionally equivalent to templated overload
template <std::size_t N2>
constexpr const_vector<T, N>& operator=(const value_type (&array)[N2]) noexcept;
constexpr void assign(size_type count, const value_type& value) noexcept; constexpr void assign(size_type count, const value_type& value) noexcept;
template<std::input_iterator InputIt> template<std::input_iterator InputIt>
@ -253,19 +257,33 @@ namespace cc {
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const const_vector &other) constexpr const_vector<T, N> &const_vector<T, N>::operator=(const const_vector &other)
{ {
if (this == &other) return *this; if (this == &other) return *this;
static_assert(N == other._len, "Cannot assign const_vector to other with different size");
assign(other.begin(), other.end()); assign(other.begin(), other.end());
return *this; return *this;
} }
template<typename T, std::size_t N>
template<std::size_t N2>
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const const_vector<const_vector::value_type, N2> &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<typename T, std::size_t N> template<typename T, std::size_t N>
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const_vector &&other) noexcept constexpr const_vector<T, N> &const_vector<T, N>::operator=(const_vector &&other) noexcept
{ {
static_assert(N == other._len, "Cannot assign const_vector to other with different size"); // cannot occur, otherwise signature would differ
if (N != other._len) throw std::exception(); //static_assert(N == other._len, "Cannot assign const_vector to other with different size");
//if (N != other._len) throw std::exception();
clear(); clear();
std::move(other.begin(), other.end(), _arr); std::move(other.begin(), other.end(), _arr);
@ -275,7 +293,8 @@ namespace cc {
} }
template<typename T, std::size_t N> template<typename T, std::size_t N>
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const value_type (&array)[N]) noexcept template<std::size_t N2>
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const value_type (&array)[N2]) noexcept
{ {
assign(std::begin(array), std::end(array)); assign(std::begin(array), std::end(array));
return *this; return *this;