From c2ec583c30cc95dd681dbb0c9fc5a46572913e22 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 10 Dec 2018 15:10:55 +0100 Subject: Update makefile ad update basic_vec to allow abstract algebraic structures Unfortunately this requires C++17 as `static constexpr` members have an undefined behaviour before this version. --- vector/makefile | 4 ++-- vector/vector.cpp | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/vector/makefile b/vector/makefile index 1303aa3..31136ea 100644 --- a/vector/makefile +++ b/vector/makefile @@ -2,8 +2,8 @@ CC := gcc CARGS := -Wall -Werror -I. LDARGS := -lm -CPPC := g++ -CPPARGS := -Wall -I. +CPPC := g++-8 +CPPARGS := -Wall -I. -std=c++17 all: vector.pdf c_build/vector cpp_build/vector diff --git a/vector/vector.cpp b/vector/vector.cpp index 9e0ca6c..d5f0c36 100644 --- a/vector/vector.cpp +++ b/vector/vector.cpp @@ -6,10 +6,15 @@ #include #include + template struct basic_vec : public std::array { static constexpr std::size_t dimensions = d; + static constexpr T null_element = static_cast(0); + static constexpr T unit_element = static_cast(1); + static constexpr T unit_additive_inverse_element = static_cast(-1); + basic_vec(); basic_vec(const std::initializer_list l); template basic_vec(const basic_vec& other); @@ -19,7 +24,7 @@ struct basic_vec : public std::array { template basic_vec::basic_vec() : std::array() { - this->fill(static_cast(0)); + this->fill(basic_vec::null_element); } template @@ -44,7 +49,7 @@ basic_vec::basic_vec(const basic_vec& other) { template T basic_vec::length() const { - T res = static_cast(0); + T res = basic_vec::null_element; for (const T& val : *this) { res += val * val; } @@ -77,12 +82,12 @@ basic_vec operator*(const T& rhs, const basic_vec& lhs) { template basic_vec operator-(const basic_vec& rhs, const basic_vec& lhs) { - return rhs + static_cast(-1) * lhs; + return rhs + basic_vec::unit_additive_inverse_element * lhs; } template T operator*(const basic_vec& rhs, const basic_vec& lhs) { - T res = static_cast(0); + T res = basic_vec::null_element; for (std::size_t i = 0; i < d; i++) { res += rhs[i] * lhs[i]; } -- cgit v1.2.1