From 2418d188615db18900ffbbfbb77ed1bc340a875c Mon Sep 17 00:00:00 2001 From: David Wells Date: Mon, 24 Jan 2022 16:08:41 -0500 Subject: [PATCH] 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. --- source/base/mpi.cc | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) 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); } -- 2.39.5