From f83342e71722208942f1f4d450717e3282b32216 Mon Sep 17 00:00:00 2001 From: Ivo Hedtke Date: Wed, 22 Nov 2023 15:41:21 +0100 Subject: [PATCH 1/2] Allow the construction of constant linear expressions without narrowing warnings --- include/scippp/lin_expr.hpp | 7 ++++++- source/lin_expr.cpp | 5 ----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/scippp/lin_expr.hpp b/include/scippp/lin_expr.hpp index cdb0813f..0d8e845e 100644 --- a/include/scippp/lin_expr.hpp +++ b/include/scippp/lin_expr.hpp @@ -29,9 +29,14 @@ class LinExpr { * Creates a linear expression with no variables. * @since 1.0.0 * @remark This is on purpose not an explicit c'tor to allow expressions like x <= 1. + * @tparam Arithmetic type that will be casted to \c double via \c static_cast * @param constant Constant term to set. */ - LinExpr(double constant); + template , bool> = true> + LinExpr(Arithmetic constant) + : m_constant { static_cast(constant) } + { + } /** * Creates a linear expression with zero as constant the given variable with coefficient one. * @since 1.0.0 diff --git a/source/lin_expr.cpp b/source/lin_expr.cpp index c3374576..858aa442 100644 --- a/source/lin_expr.cpp +++ b/source/lin_expr.cpp @@ -2,11 +2,6 @@ namespace scippp { -LinExpr::LinExpr(double constant) - : m_constant { constant } -{ -} - LinExpr::LinExpr(const Var& var) : m_vars { var } , m_coeffs(1, 1) From 4baa55ea58d42cefe73f54da9fbe2218f46a81c0 Mon Sep 17 00:00:00 2001 From: Ivo Hedtke Date: Wed, 22 Nov 2023 15:52:22 +0100 Subject: [PATCH 2/2] Add explicit warning on narrowing in LinExpr --- include/scippp/lin_expr.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/scippp/lin_expr.hpp b/include/scippp/lin_expr.hpp index 0d8e845e..e0c7f6f7 100644 --- a/include/scippp/lin_expr.hpp +++ b/include/scippp/lin_expr.hpp @@ -29,7 +29,8 @@ class LinExpr { * Creates a linear expression with no variables. * @since 1.0.0 * @remark This is on purpose not an explicit c'tor to allow expressions like x <= 1. - * @tparam Arithmetic type that will be casted to \c double via \c static_cast + * @tparam Arithmetic type that will be casted to \c double. + * @warning \c Arithmetic is casted to \c double without creating a narrowing warning! * @param constant Constant term to set. */ template , bool> = true>