From: David Wells Date: Mon, 24 Jan 2022 21:08:41 +0000 (-0500) Subject: Fix a bogus warning about a null pointer in a lambda. X-Git-Tag: v9.4.0-rc1~570^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F13289%2Fhead;p=dealii.git Fix a bogus warning about a null pointer in a lambda. I get the following warning: /home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc: In static member function 'static constexpr void dealii::Utilities::MPI::create_mpi_data_type_n_bytes(std::size_t)::::_FUN(ompi_datatype_t**)': /home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc:384:15: warning: 'this' pointer is null [-Wnonnull] 384 | }}; | ^ /home/drwells/Documents/Code/CPP/dealii-dev/source/base/mpi.cc:376:15: note: in a call to non-static member function 'dealii::Utilities::MPI::create_mpi_data_type_n_bytes(std::size_t)::' 376 | [](MPI_Datatype *p) { | ^ This isn't a problem since deleters don't store any kind of state but we can work around it by splitting the constructor call. --- diff --git a/source/base/mpi.cc b/source/base/mpi.cc index 15f101bad7..ed78d4f6e2 100644 --- a/source/base/mpi.cc +++ b/source/base/mpi.cc @@ -369,19 +369,22 @@ namespace Utilities // object, and as second argument a pointer-to-function, for which // we here use a lambda function without captures that acts as the // 'deleter' object: it calls `MPI_Type_free` and then deletes the - // pointer. - return {// The copy of the object: - new MPI_Datatype(result), - // The deleter: - [](MPI_Datatype *p) { - if (p != nullptr) - { - const int ierr = MPI_Type_free(p); - AssertThrowMPI(ierr); - - delete p; - } - }}; + // pointer. To avoid a compiler warning about a null this pointer + // in the lambda (which don't make sense: the lambda doesn't store + // anything), we create the deleter first. + auto deleter = [](MPI_Datatype *p) { + if (p != nullptr) + { + const int ierr = MPI_Type_free(p); + (void)ierr; + AssertNothrow(ierr == MPI_SUCCESS, ExcMPI(ierr)); + + delete p; + } + }; + + return std::unique_ptr( + new MPI_Datatype(result), deleter); }