From: Daniel Arndt Date: Mon, 7 Mar 2022 20:33:17 +0000 (-0500) Subject: Use holds_alternative and some more fixes X-Git-Tag: v9.4.0-rc1~404^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F13502%2Fhead;p=dealii.git Use holds_alternative and some more fixes --- diff --git a/include/deal.II/base/std_cxx17/variant.h b/include/deal.II/base/std_cxx17/variant.h index f7f3ec2792..f32ecaa276 100644 --- a/include/deal.II/base/std_cxx17/variant.h +++ b/include/deal.II/base/std_cxx17/variant.h @@ -31,8 +31,16 @@ namespace std_cxx17 #ifndef DEAL_II_HAVE_CXX17 using boost::get; using boost::variant; + + template + bool + holds_alternative(const boost::variant &v) noexcept + { + return boost::get(&v) != nullptr; + } #else using std::get; + using std::holds_alternative; using std::variant; #endif } // namespace std_cxx17 diff --git a/include/deal.II/base/table_handler.h b/include/deal.II/base/table_handler.h index 9c7974e18c..a0d2b524c7 100644 --- a/include/deal.II/base/table_handler.h +++ b/include/deal.II/base/table_handler.h @@ -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(&value)) + if (std_cxx17::holds_alternative(value)) { - char c = 'i'; - ar &c &*p; + const int p = std_cxx17::get(value); + char c = 'i'; + ar &c &p; } - else if (const unsigned int *p = std_cxx17::get(&value)) + else if (std_cxx17::holds_alternative(value)) { - char c = 'u'; - ar &c &*p; + const unsigned int p = std_cxx17::get(value); + char c = 'u'; + ar &c &p; } - else if (const double *p = std_cxx17::get(&value)) + else if (std_cxx17::holds_alternative(value)) { - char c = 'd'; - ar &c &*p; + const double p = std_cxx17::get(value); + char c = 'd'; + ar &c &p; } - else if (const std::string *p = std_cxx17::get(&value)) + else if (std_cxx17::holds_alternative(value)) { - char c = 's'; - ar &c &*p; + const std::string p = std_cxx17::get(value); + char c = 's'; + ar &c &p; } - else if (const std::uint64_t *p = std_cxx17::get(&value)) + else if (std_cxx17::holds_alternative(value)) { - char c = 'l'; - ar &c &*p; + const std::uint64_t p = std_cxx17::get(value); + char c = 'l'; + ar &c &p; } else Assert(false, ExcInternalError()); diff --git a/source/base/table_handler.cc b/source/base/table_handler.cc index 86df306755..d4b3db9a7e 100644 --- a/source/base/table_handler.cc +++ b/source/base/table_handler.cc @@ -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(); }, + 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 << " & ";