]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Template Utilities::pow() 9764/head
authorPeter Munch <peterrmuench@gmail.com>
Sat, 28 Mar 2020 22:05:21 +0000 (23:05 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Sat, 28 Mar 2020 22:20:14 +0000 (23:20 +0100)
include/deal.II/base/utilities.h

index 3a16df7d51cab3342c0f6b58d0fd0d881453bb0e..a1db0354649a8cf8260422d7dcf40815a947d3ad 100644 (file)
@@ -480,11 +480,12 @@ namespace Utilities
 
   /**
    * A replacement for <code>std::pow</code> that allows compile-time
-   * calculations for constant expression arguments. The exponent @p iexp
-   * must not be negative.
+   * calculations for constant expression arguments. The @p base must
+   * be an integer type and the exponent @p iexp must not be negative.
    */
-  constexpr unsigned int
-  pow(const unsigned int base, const int iexp)
+  template <typename T>
+  constexpr T
+  pow(const T base, const int iexp)
   {
 #if defined(DEBUG) && defined(DEAL_II_HAVE_CXX14_CONSTEXPR)
     // Up to __builtin_expect this is the same code as in the 'Assert' macro.
@@ -516,6 +517,8 @@ namespace Utilities
     // return prefactor * dealii::Utilities::pow(base*base, iexp/2);
     // </code>
 
+    static_assert(std::is_integral<T>::value, "Only integral types supported");
+
     return iexp <= 0 ? 1 :
                        (((iexp % 2 == 1) ? base : 1) *
                         dealii::Utilities::pow(base * base, iexp / 2));

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.