]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use holds_alternative and some more fixes 13502/head
authorDaniel Arndt <arndtd@ornl.gov>
Mon, 7 Mar 2022 20:33:17 +0000 (15:33 -0500)
committerDaniel Arndt <arndtd@ornl.gov>
Mon, 7 Mar 2022 20:57:21 +0000 (15:57 -0500)
include/deal.II/base/std_cxx17/variant.h
include/deal.II/base/table_handler.h
source/base/table_handler.cc

index f7f3ec279270a9a1378f18d82412eaf3898894f0..f32ecaa276ddfe050a88781e3a657a5ff320f7c5 100644 (file)
@@ -31,8 +31,16 @@ namespace std_cxx17
 #ifndef DEAL_II_HAVE_CXX17
   using boost::get;
   using boost::variant;
+
+  template <typename T, typename... Ts>
+  bool
+  holds_alternative(const boost::variant<Ts...> &v) noexcept
+  {
+    return boost::get<T>(&v) != nullptr;
+  }
 #else
   using std::get;
+  using std::holds_alternative;
   using std::variant;
 #endif
 } // namespace std_cxx17
index 9c7974e18c0c888d2438f03761b84ef1d0dca634..a0d2b524c75d761bb48773a2b9fe060a95b418e7 100644 (file)
@@ -846,30 +846,35 @@ namespace internal
     // write first an identifier for the kind
     // of data stored and then the actual
     // data, in its correct data type
-    if (const int *p = std_cxx17::get<int>(&value))
+    if (std_cxx17::holds_alternative<int>(value))
       {
-        char c = 'i';
-        ar &c &*p;
+        const int p = std_cxx17::get<int>(value);
+        char      c = 'i';
+        ar &c &p;
       }
-    else if (const unsigned int *p = std_cxx17::get<unsigned int>(&value))
+    else if (std_cxx17::holds_alternative<unsigned int>(value))
       {
-        char c = 'u';
-        ar &c &*p;
+        const unsigned int p = std_cxx17::get<unsigned int>(value);
+        char               c = 'u';
+        ar &c &p;
       }
-    else if (const double *p = std_cxx17::get<double>(&value))
+    else if (std_cxx17::holds_alternative<double>(value))
       {
-        char c = 'd';
-        ar &c &*p;
+        const double p = std_cxx17::get<double>(value);
+        char         c = 'd';
+        ar &c &p;
       }
-    else if (const std::string *p = std_cxx17::get<std::string>(&value))
+    else if (std_cxx17::holds_alternative<std::string>(value))
       {
-        char c = 's';
-        ar &c &*p;
+        const std::string p = std_cxx17::get<std::string>(value);
+        char              c = 's';
+        ar &c &p;
       }
-    else if (const std::uint64_t *p = std_cxx17::get<std::uint64_t>(&value))
+    else if (std_cxx17::holds_alternative<std::uint64_t>(value))
       {
-        char c = 'l';
-        ar &c &*p;
+        const std::uint64_t p = std_cxx17::get<std::uint64_t>(value);
+        char                c = 'l';
+        ar &c &p;
       }
     else
       Assert(false, ExcInternalError());
index 86df306755ac50aa9c433031a7d1c097c52637f4..d4b3db9a7e63aa63321a3015c19e62beeb93fdcf 100644 (file)
@@ -91,7 +91,11 @@ namespace internal
     else
       ss.setf(std::ios::fixed, std::ios::floatfield);
 
+#ifdef DEAL_II_HAVE_CXX17
+    std::visit([&ss](auto &v) { ss << v; }, value);
+#else
     ss << value;
+#endif
 
     cached_value = ss.str();
     if (cached_value.size() == 0)
@@ -132,7 +136,9 @@ namespace internal
     // Let std::visit figure out which data type is actually stored,
     // and then set the object so stored to a default-constructed
     // one.
-    std::visit([](auto &arg) { arg = decltype(arg)(); }, new_entry.value);
+    std::visit([](
+                 auto &arg) { arg = std::remove_reference_t<decltype(arg)>(); },
+               new_entry.value);
 #endif
 
     return new_entry;
@@ -722,7 +728,11 @@ TableHandler::write_tex(std::ostream &out, const bool with_header) const
           else
             out.setf(std::ios::fixed, std::ios::floatfield);
 
+#ifdef DEAL_II_HAVE_CXX17
+          std::visit([&out](auto &v) { out << v; }, column.entries[i].value);
+#else
           out << column.entries[i].value;
+#endif
 
           if (j < n_cols - 1)
             out << " & ";

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.