[modernize] Use if constexpr and std::clamp in TinySolver

Change-Id: I4218bfea314a2438cceb7eaf49a2ec921aaa3e45
diff --git a/include/ceres/tiny_solver.h b/include/ceres/tiny_solver.h
index f5cdfd8..69f181b 100644
--- a/include/ceres/tiny_solver.h
+++ b/include/ceres/tiny_solver.h
@@ -51,6 +51,7 @@
 #ifndef CERES_PUBLIC_TINY_SOLVER_H_
 #define CERES_PUBLIC_TINY_SOLVER_H_
 
+#include <algorithm>
 #include <cassert>
 #include <cmath>
 
@@ -250,7 +251,7 @@
       const Scalar max_diagonal = 1e32;
       for (int i = 0; i < dx_.rows(); ++i) {
         jtj_regularized_(i, i) +=
-            u * (std::min)((std::max)(jtj_(i, i), min_diagonal), max_diagonal);
+            u * std::clamp(jtj_(i, i), min_diagonal, max_diagonal);
       }
 
       // TODO(sameeragarwal): Check for failure and deal with it.
@@ -351,43 +352,17 @@
   Eigen::Matrix<Scalar, NUM_RESIDUALS, NUM_PARAMETERS> jacobian_;
   Eigen::Matrix<Scalar, NUM_PARAMETERS, NUM_PARAMETERS> jtj_, jtj_regularized_;
 
-  // The following definitions are needed for template metaprogramming.
-  template <bool Condition, typename T>
-  struct enable_if;
-
-  template <typename T>
-  struct enable_if<true, T> {
-    using type = T;
-  };
-
-  // The number of parameters and residuals are dynamically sized.
   template <int R, int P>
-  typename enable_if<(R == Eigen::Dynamic && P == Eigen::Dynamic), void>::type
-  Initialize(const Function& function) {
-    Initialize(function.NumResiduals(), function.NumParameters());
+  void Initialize(const Function& function) {
+    if constexpr (R == Eigen::Dynamic && P == Eigen::Dynamic) {
+      Initialize(function.NumResiduals(), function.NumParameters());
+    } else if constexpr (R == Eigen::Dynamic && P != Eigen::Dynamic) {
+      Initialize(function.NumResiduals(), P);
+    } else if constexpr (R != Eigen::Dynamic && P == Eigen::Dynamic) {
+      Initialize(R, function.NumParameters());
+    }
   }
 
-  // The number of parameters is dynamically sized and the number of
-  // residuals is statically sized.
-  template <int R, int P>
-  typename enable_if<(R == Eigen::Dynamic && P != Eigen::Dynamic), void>::type
-  Initialize(const Function& function) {
-    Initialize(function.NumResiduals(), P);
-  }
-
-  // The number of parameters is statically sized and the number of
-  // residuals is dynamically sized.
-  template <int R, int P>
-  typename enable_if<(R != Eigen::Dynamic && P == Eigen::Dynamic), void>::type
-  Initialize(const Function& function) {
-    Initialize(R, function.NumParameters());
-  }
-
-  // The number of parameters and residuals are statically sized.
-  template <int R, int P>
-  typename enable_if<(R != Eigen::Dynamic && P != Eigen::Dynamic), void>::type
-  Initialize(const Function& /* function */) {}
-
   void Initialize(int num_residuals, int num_parameters) {
     dx_.resize(num_parameters);
     x_new_.resize(num_parameters);
diff --git a/include/ceres/tiny_solver_autodiff_function.h b/include/ceres/tiny_solver_autodiff_function.h
index 1b9bd96..fa67538 100644
--- a/include/ceres/tiny_solver_autodiff_function.h
+++ b/include/ceres/tiny_solver_autodiff_function.h
@@ -184,20 +184,14 @@
   // Eigen::Matrix serves as static or dynamic container.
   mutable Eigen::Matrix<JetType, kNumResiduals, 1> jet_residuals_;
 
-  // The number of residuals is dynamically sized and the number of
-  // parameters is statically sized.
   template <int R>
-  typename std::enable_if<(R == Eigen::Dynamic), void>::type Initialize(
-      const CostFunctor& function) {
-    jet_residuals_.resize(function.NumResiduals());
-    num_residuals_ = function.NumResiduals();
-  }
-
-  // The number of parameters and residuals are statically sized.
-  template <int R>
-  typename std::enable_if<(R != Eigen::Dynamic), void>::type Initialize(
-      const CostFunctor& /* function */) {
-    num_residuals_ = kNumResiduals;
+  void Initialize(const CostFunctor& function) {
+    if constexpr (R == Eigen::Dynamic) {
+      jet_residuals_.resize(function.NumResiduals());
+      num_residuals_ = function.NumResiduals();
+    } else {
+      num_residuals_ = kNumResiduals;
+    }
   }
 };