From 7e17d196eb888829dbfe31a7c0616350ba8114e8 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Thu, 7 Feb 2019 18:17:53 +0100 Subject: Add move constructor and move operator= to mmvec --- include/mm/mmvec.hpp | 34 ++++++++++++++++++++++++++++------ 1 file 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 { basic_vec(); basic_vec(const std::initializer_list l); + + // copyable to a vector of size n <= d template basic_vec(const basic_vec& other); + // movable to a vector of size n <= d + template basic_vec(basic_vec&& other); T length() const; + // copy operator= template basic_vec& operator=(const mm::basic_vec& other); + // move operator= + template + basic_vec& operator=(mm::basic_vec&& other); + template basic_vec& operator+=(const mm::basic_vec& other); @@ -78,9 +87,6 @@ mm::basic_vec::basic_vec() : std::array() { template mm::basic_vec::basic_vec(const std::initializer_list 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::basic_vec(const std::initializer_list l) { template template mm::basic_vec::basic_vec(const mm::basic_vec& other) { - // construct with empty values - basic_vec(); - // uses operator= + *this = other; +} + +template +template +mm::basic_vec::basic_vec(basic_vec&& other) { *this = other; } @@ -120,6 +129,19 @@ mm::basic_vec& mm::basic_vec::operator=(const mm::basic_vec& o return *this; } +template +template +mm::basic_vec& mm::basic_vec::operator=(mm::basic_vec&& 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 template mm::basic_vec& mm::basic_vec::operator+=(const mm::basic_vec& other) { -- cgit v1.2.1