From 31d6094e306bc58a7ec1e8017c40c45c82645bc4 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 24 Aug 2018 11:29:03 +0200 Subject: [PATCH] Assert positive exponents in Utilities::pow --- include/deal.II/base/utilities.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index bcb1e8a33f..e469558f59 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -347,12 +347,18 @@ namespace Utilities /** * A replacement for std::pow that allows compile-time - * calculations for constant expression arguments. + * calculations for constant expression arguments. The exponent @p iexp + * must not be negative. */ constexpr unsigned int - pow(const unsigned int base, const unsigned int iexp) + pow(const unsigned int base, const int iexp) { - return iexp == 0 ? 1 : base * dealii::Utilities::pow(base, iexp - 1); +#ifdef DEAL_II_WITH_CXX17 + AssertThrow(iexp >= 0, ExcMessage("The exponent must not be negative!")); +#endif + return iexp <= 0 ? 1 : + (((iexp % 2 == 1) ? base : 1) * + dealii::Utilities::pow(base * base, iexp / 2)); } /** -- 2.39.5