From e1d46c1570ec7d77c8ffff0e3dbc1feaad33ca15 Mon Sep 17 00:00:00 2001 From: Sean Kim Date: Tue, 15 Jul 2025 19:12:34 +0200 Subject: [PATCH] BLI: Ensure `rcp` and `safe_rcp` are not used with integral types Since we expect the result of `rcp` and `safe_rcp` to always be in the range of [0, 1], it doesn't make sense to allow passing in integer types without casting them, as this has a high possibility of introducing unwanted behavior by only returning either 0 or 1. To prevent this, this commit adds a static assert on the type. Pull Request: https://projects.blender.org/blender/blender/pulls/141567 --- source/blender/blenlib/BLI_math_base.hh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/blender/blenlib/BLI_math_base.hh b/source/blender/blenlib/BLI_math_base.hh index 864c338cc1c..47295ffc1ba 100644 --- a/source/blender/blenlib/BLI_math_base.hh +++ b/source/blender/blenlib/BLI_math_base.hh @@ -149,6 +149,7 @@ template inline T sqrt(const T &a) * If the input is zero the output is NaN. */ template inline T rcp(const T &a) { + static_assert(!std::is_integral_v, "T must not be an integral type."); return T(1) / a; } @@ -156,6 +157,7 @@ template inline T rcp(const T &a) * If the input is zero the output is zero. */ template inline T safe_rcp(const T &a) { + static_assert(!std::is_integral_v, "T must be not be an integral type."); return a ? T(1) / a : T(0); }