Skip to content

Commit

Permalink
fix(templates): patches to avoid weird issues in g++
Browse files Browse the repository at this point in the history
  • Loading branch information
STommydx committed Apr 5, 2024
1 parent adb534e commit e88abf5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifndef MATRIX_HPP
#define MATRIX_HPP

#include <algorithm>
#include <concepts>
#include <functional>
#include <utility>
#include <valarray>
#include <vector>
Expand Down Expand Up @@ -219,11 +222,6 @@ template <class T> class matrix {
T &operator()(size_t i, size_t j) { return at(i, j); }
operator std::vector<std::vector<T>>() const { return to_vector(); }

matrix operator+() const { return matrix(n, m, +dat); }
matrix operator-() const { return matrix(n, m, -dat); }
matrix operator~() const { return matrix(n, m, ~dat); }
matrix<bool> operator!() const { return matrix<bool>(n, m, !dat); }

#define MEMBER_BINARY_OP(OP) \
matrix &operator OP(const matrix & m) { \
dat OP m.dat; \
Expand All @@ -250,9 +248,9 @@ template <class T> class matrix {
* Non-member functions
*/
#define NON_MEMBER_BINARY_OP(OP) \
friend matrix<T> operator OP<>(const matrix<T> &a, const matrix<T> &b); \
friend matrix<T> operator OP<>(const matrix<T> &a, const T & b); \
friend matrix<T> operator OP<>(const T & a, const matrix<T> &b);
friend matrix<T> operator OP<T>(const matrix<T> &a, const matrix<T> &b); \
friend matrix<T> operator OP<T>(const matrix<T> &a, const T & b); \
friend matrix<T> operator OP<T>(const T & a, const matrix<T> &b);

NON_MEMBER_BINARY_OP(+)
NON_MEMBER_BINARY_OP(-)
Expand All @@ -267,9 +265,10 @@ template <class T> class matrix {
#undef NON_MEMBER_BINARY_OP

#define NON_MEMBER_BINARY_PREDICATE(OP) \
friend matrix<bool> operator OP<>(const matrix<T> &a, const matrix<T> &b); \
friend matrix<bool> operator OP<>(const matrix<T> &a, const T & b); \
friend matrix<bool> operator OP<>(const T & a, const matrix<T> &b);
friend matrix<bool> operator OP<T>(const matrix<T> &a, \
const matrix<T> &b); \
friend matrix<bool> operator OP<T>(const matrix<T> &a, const T & b); \
friend matrix<bool> operator OP<T>(const T & a, const matrix<T> &b);

NON_MEMBER_BINARY_PREDICATE(&&)
NON_MEMBER_BINARY_PREDICATE(||)
Expand All @@ -281,6 +280,14 @@ template <class T> class matrix {
NON_MEMBER_BINARY_PREDICATE(>=)
#undef NON_MEMBER_BINARY_PREDICATE

matrix operator+() const { return matrix(n, m, +dat); }
matrix operator-() const { return matrix(n, m, -dat); }
matrix operator~() const { return matrix(n, m, ~dat); }
matrix<bool> operator!() const { return matrix<bool>(n, m, !dat); }

/*
* Other non-member friend functions
*/
friend matrix matmul<>(const matrix &a, const matrix &b);
};

Expand Down
2 changes: 1 addition & 1 deletion matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ TEST_CASE("matrix construction and member functions", "[matrix]") {
REQUIRE(std::ranges::equal(a.sum<0>(), std::vector<int>{3, 1}));
REQUIRE(std::ranges::equal(a.sum<1>(), std::vector<int>{2, 2}));
auto aggregated = a.sum<0, true>();
REQUIRE(aggregated.shape() == std::make_pair(1, 2));
REQUIRE(aggregated.shape() == std::pair<size_t, size_t>(1, 2));
REQUIRE(std::ranges::equal(std::valarray<int>(aggregated.row(0)),
std::vector<int>{3, 1}));
REQUIRE(a.max() == 2);
Expand Down

0 comments on commit e88abf5

Please sign in to comment.