libcamera: matrix: Replace SFINAE with static_asserts

SFINAE is difficult to read and not needed in these cases. Replace it
with static_asserts. The idea came from [1] where it is stated:

"The use of enable_if seems misguided to me. SFINAE is useful for the
situation where we consider multiple candidates for something (overloads
or class template specializations) and try to choose the correct one,
without causing compilation to fail."

[1]: https://stackoverflow.com/questions/62109526/c-friend-template-that-use-sfinae

Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
This commit is contained in:
Stefan Klug 2025-04-03 17:49:06 +02:00
parent 0069b9ceb1
commit 1d8a6db31c

View file

@ -19,14 +19,11 @@ namespace libcamera {
LOG_DECLARE_CATEGORY(Matrix) LOG_DECLARE_CATEGORY(Matrix)
#ifndef __DOXYGEN__
template<typename T, unsigned int Rows, unsigned int Cols,
std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
#else
template<typename T, unsigned int Rows, unsigned int Cols> template<typename T, unsigned int Rows, unsigned int Cols>
#endif /* __DOXYGEN__ */
class Matrix class Matrix
{ {
static_assert(std::is_arithmetic_v<T>, "Matrix type must be arithmetic");
public: public:
Matrix() Matrix()
{ {
@ -123,16 +120,10 @@ Matrix<U, Rows, Cols> operator*(const Matrix<U, Rows, Cols> &m, T d)
return d * m; return d * m;
} }
#ifndef __DOXYGEN__ template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned int C2>
template<typename T, constexpr Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)
unsigned int R1, unsigned int C1,
unsigned int R2, unsigned int C2,
std::enable_if_t<C1 == R2> * = nullptr>
#else
template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned in C2>
#endif /* __DOXYGEN__ */
Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)
{ {
static_assert(C1 == R2, "Matrix dimensions must match for multiplication");
Matrix<T, R1, C2> result; Matrix<T, R1, C2> result;
for (unsigned int i = 0; i < R1; i++) { for (unsigned int i = 0; i < R1; i++) {