From patchwork Wed Mar 19 16:11:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22979 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 20986C32FE for ; Wed, 19 Mar 2025 16:12:02 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CCBB868950; Wed, 19 Mar 2025 17:12:01 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UsT7bEDo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F24CD68945 for ; Wed, 19 Mar 2025 17:11:59 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:760:e5ca:4814:99c7]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 04DBE8FA; Wed, 19 Mar 2025 17:10:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742400617; bh=oFIl/d/8UPVKsZ0KLKeqRsE/24LTr+AMeld281uk9Cw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UsT7bEDoBvpHt1cIj7RiEusG2Ettnm1gbXeJaOLl6hLSpvzec+dskJzdsBe58RseC hAG1m+y25gcs03vS6034jYW8AysI7X3WETTquMpIUgMkAvDUPczb8Jrv4dO5gfDpGj s5wSkx3D1oVCM971lJLHP/ObbVt5D8jA2Z8jfyo8= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH v2 01/17] libcamera: matrix: Replace SFINAE with static_asserts Date: Wed, 19 Mar 2025 17:11:06 +0100 Message-ID: <20250319161152.63625-2-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250319161152.63625-1-stefan.klug@ideasonboard.com> References: <20250319161152.63625-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- Changes in v2: - Added this patch --- include/libcamera/internal/matrix.h | 42 ++++++++--------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index a055e6926c94..8399be583f28 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -19,14 +19,11 @@ namespace libcamera { LOG_DECLARE_CATEGORY(Matrix) -#ifndef __DOXYGEN__ -template> * = nullptr> -#else template -#endif /* __DOXYGEN__ */ class Matrix { + static_assert(std::is_arithmetic_v, "Matrix type must be arithmetic"); + public: Matrix() { @@ -78,13 +75,10 @@ public: return Span{ &data_.data()[i * Cols], Cols }; } -#ifndef __DOXYGEN__ - template>> -#else template -#endif /* __DOXYGEN__ */ - Matrix &operator*=(U d) + constexpr Matrix &operator*=(U d) { + static_assert(std::is_arithmetic_v, "Multiplier must be arithmetic"); for (unsigned int i = 0; i < Rows * Cols; i++) data_[i] *= d; return *this; @@ -94,14 +88,10 @@ private: std::array data_; }; -#ifndef __DOXYGEN__ -template> * = nullptr> -#else template -#endif /* __DOXYGEN__ */ -Matrix operator*(T d, const Matrix &m) +constexpr Matrix operator*(T d, const Matrix &m) { + static_assert(std::is_arithmetic_v, "Multiplier must be arithmetic"); Matrix result; for (unsigned int i = 0; i < Rows; i++) { @@ -112,27 +102,17 @@ Matrix operator*(T d, const Matrix &m) return result; } -#ifndef __DOXYGEN__ -template> * = nullptr> -#else template -#endif /* __DOXYGEN__ */ -Matrix operator*(const Matrix &m, T d) +constexpr Matrix operator*(const Matrix &m, T d) { + static_assert(std::is_arithmetic_v, "Multiplier must be arithmetic"); return d * m; } -#ifndef __DOXYGEN__ -template * = nullptr> -#else -template -#endif /* __DOXYGEN__ */ -Matrix operator*(const Matrix &m1, const Matrix &m2) +template +constexpr Matrix operator*(const Matrix &m1, const Matrix &m2) { + static_assert(C1 == R2, "Matrix dimensions must match for multiplication"); Matrix result; for (unsigned int i = 0; i < R1; i++) {