From: Wolfgang Bangerth Date: Sat, 15 Jan 2022 13:51:26 +0000 (-0700) Subject: Refactor a lambda function into a free function. X-Git-Tag: v9.4.0-rc1~605^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ac08067f910d7df0085e7bc834b9812711a9012;p=dealii.git Refactor a lambda function into a free function. --- diff --git a/include/deal.II/base/mpi_consensus_algorithms.templates.h b/include/deal.II/base/mpi_consensus_algorithms.templates.h index e7982b81d4..b0c2993bed 100644 --- a/include/deal.II/base/mpi_consensus_algorithms.templates.h +++ b/include/deal.II/base/mpi_consensus_algorithms.templates.h @@ -39,6 +39,39 @@ namespace Utilities { namespace ConsensusAlgorithms { + namespace + { + /** + * Return whether a vector of targets (MPI ranks) has only unique + * elements. + * + * This function is only used within assertions, which causes GCC + * to issue a warning in release mode that due to -Werror then causes an + * error. We suppress this by using the [[gnu::unused]] error (because + * the + * [[maybe_unused]] attribute is only supported from C++17 forward). + * + * Unfortunately, in contrast to what the standard says, the Microsoft + * compiler does not ignore the gnu::unused attribute as it should, + * and then produces an error of its own. So we disable the attribute + * for that compiler. + */ +#ifndef DEAL_II_MSVC + [[gnu::unused]] +#endif + bool + has_unique_elements(const std::vector &targets) + { + std::vector my_destinations = targets; + std::sort(my_destinations.begin(), my_destinations.end()); + return (std::adjacent_find(my_destinations.begin(), + my_destinations.end()) == + my_destinations.end()); + } + } // namespace + + + template void Process::answer_request(const unsigned int, @@ -151,19 +184,10 @@ namespace Utilities #ifdef DEAL_II_WITH_MPI // 1) targets = this->process.compute_targets(); - Assert( - [this]() { - std::vector my_destinations = this->targets; - const unsigned int n_destinations = my_destinations.size(); - std::sort(my_destinations.begin(), my_destinations.end()); - my_destinations.erase(std::unique(my_destinations.begin(), - my_destinations.end()), - my_destinations.end()); - return (my_destinations.size() == n_destinations); - }(), - ExcMessage("The consensus algorithms expect that each process " - "only sends a single message to another process, " - "but the targets provided include duplicates.")); + Assert(has_unique_elements(targets), + ExcMessage("The consensus algorithms expect that each process " + "only sends a single message to another process, " + "but the targets provided include duplicates.")); const auto n_targets = targets.size(); @@ -451,19 +475,10 @@ namespace Utilities // 1) determine with which processes this process wants to communicate // with targets = this->process.compute_targets(); - Assert( - [this]() { - std::vector my_destinations = this->targets; - const unsigned int n_destinations = my_destinations.size(); - std::sort(my_destinations.begin(), my_destinations.end()); - my_destinations.erase(std::unique(my_destinations.begin(), - my_destinations.end()), - my_destinations.end()); - return (my_destinations.size() == n_destinations); - }(), - ExcMessage("The consensus algorithms expect that each process " - "only sends a single message to another process, " - "but the targets provided include duplicates.")); + Assert(has_unique_elements(targets), + ExcMessage("The consensus algorithms expect that each process " + "only sends a single message to another process, " + "but the targets provided include duplicates.")); // 2) determine who wants to communicate with this process sources =