I created a simple 4x4 matrix class (column-major). I would like it to be efficient and to use C++14's full capabilities. Can I improve it?
#include <array>
class mat4
{
public:
constexpr mat4() noexcept : matrix() {}
constexpr mat4(const std::array<float, 16> &m) noexcept : matrix(m) {}
constexpr mat4(std::array<float, 16> &&m) noexcept : matrix(std::move(m)) {}
constexpr mat4(const mat4 &other) noexcept : matrix(other.matrix) {}
constexpr mat4(mat4 &&other) noexcept : matrix(std::move(other.matrix)) {}
constexpr bool operator==(const mat4 &other) const noexcept
{
for (size_t i = 0; i < 16; ++i) {
if ((*this)[i] != other[i]) {
return false;
}
}
return true;
}
constexpr bool operator!=(const mat4 &other) const noexcept
{
return !(this->operator==(other));
}
constexpr mat4& operator+=(const mat4 &other) noexcept
{
for (size_t i = 0; i < 16; ++i) {
(*this)[i] += other[i];
}
return *this;
}
constexpr mat4& operator*=(float scalar) noexcept
{
for (size_t i = 0; i < 16; ++i) {
(*this)[i] *= scalar;
}
return *this;
}
mat4& operator=(mat4 other) noexcept
{
std::swap(this->matrix, other.matrix);
return *this;
}
constexpr float& operator[](size_t index) { return const_cast<float&>(static_cast<const std::array<float, 16>&>(matrix)[index]); }
constexpr float operator[](size_t index) const { return matrix[index]; }
void print() const noexcept
{
printf("\n");
printf("[%.2f][%.2f][%.2f][%.2f]\n", matrix[0], matrix[4], matrix[8], matrix[12]);
printf("[%.2f][%.2f][%.2f][%.2f]\n", matrix[1], matrix[5], matrix[9], matrix[13]);
printf("[%.2f][%.2f][%.2f][%.2f]\n", matrix[2], matrix[6], matrix[10], matrix[14]);
printf("[%.2f][%.2f][%.2f][%.2f]\n", matrix[3], matrix[7], matrix[11], matrix[15]);
}
private:
std::array<float, 16> matrix;
};
constexpr const mat4 operator+(mat4 lhs, const mat4 &rhs) noexcept
{
lhs += rhs;
return lhs;
}
constexpr const mat4 operator*(const mat4 &lhs, const mat4 &rhs) noexcept
{
mat4 result;
for (size_t i = 0; i < 4; ++i)
{
for (size_t j = 0; j < 4; ++j)
{
for (size_t k = 0; k < 4; ++k) {
result[i + 4 * j] += lhs[i + 4 * k] * rhs[k + 4 * j];
}
}
}
return result;
}
constexpr const mat4 operator*(mat4 lhs, float scalar) noexcept
{
lhs *= scalar;
return lhs;
}
You can test it in this live demo here.
constand the other not. \$\endgroup\$