diff options
author | Nao Pross <naopross@thearcway.org> | 2019-02-07 18:17:53 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-02-07 18:18:04 +0100 |
commit | 7e17d196eb888829dbfe31a7c0616350ba8114e8 (patch) | |
tree | 6ef0d6753b4fd48bc4bfc2f848aaf40c12836826 | |
parent | Change public headers directory (include) structure (diff) | |
download | libmm-7e17d196eb888829dbfe31a7c0616350ba8114e8.tar.gz libmm-7e17d196eb888829dbfe31a7c0616350ba8114e8.zip |
Add move constructor and move operator= to mmvec
-rw-r--r-- | include/mm/mmvec.hpp | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/include/mm/mmvec.hpp b/include/mm/mmvec.hpp index 4bac658..db3c390 100644 --- a/include/mm/mmvec.hpp +++ b/include/mm/mmvec.hpp @@ -51,13 +51,22 @@ struct mm::basic_vec : public std::array<T, d> { basic_vec(); basic_vec(const std::initializer_list<T> l); + + // copyable to a vector of size n <= d template<std::size_t n> basic_vec(const basic_vec<T, n>& other); + // movable to a vector of size n <= d + template<std::size_t n> basic_vec(basic_vec<T, n>&& other); T length() const; + // copy operator= template<std::size_t n> basic_vec<T, d>& operator=(const mm::basic_vec<T, n>& other); + // move operator= + template<std::size_t n> + basic_vec<T, d>& operator=(mm::basic_vec<T, n>&& other); + template<std::size_t n> basic_vec<T, d>& operator+=(const mm::basic_vec<T, n>& other); @@ -78,9 +87,6 @@ mm::basic_vec<T, d>::basic_vec() : std::array<T, d>() { template<typename T, std::size_t d> mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) { - // construct with empty values - basic_vec(); - // why can't this sh*t be a constexpr with static_assert??? assert(l.size() <= d); std::copy(l.begin(), l.end(), this->begin()); @@ -89,9 +95,12 @@ mm::basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) { template<typename T, std::size_t d> template<std::size_t n> mm::basic_vec<T, d>::basic_vec(const mm::basic_vec<T, n>& other) { - // construct with empty values - basic_vec(); - // uses operator= + *this = other; +} + +template<typename T, std::size_t d> +template<std::size_t n> +mm::basic_vec<T, d>::basic_vec(basic_vec<T, n>&& other) { *this = other; } @@ -122,6 +131,19 @@ mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(const mm::basic_vec<T, n>& o template<typename T, std::size_t d> template<std::size_t n> +mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator=(mm::basic_vec<T, n>&& other) { + static_assert( + d >= n, "cannot move a higher dimensional vector into a smaller one" + ); + + std::move(other.begin(), other.end(), this->begin()); + + return *this; +} + + +template<typename T, std::size_t d> +template<std::size_t n> mm::basic_vec<T, d>& mm::basic_vec<T, d>::operator+=(const mm::basic_vec<T, n>& other) { *this = *this + other; return *this; |