Skip to content

[#2311] fix warning regarding capture of ‘this’

Andrei Pavel requested to merge 2311-fix-warning into master

Closes #2311 (closed).

The following warnings happen only when NETCONF is disabled and only on some compilers.

The timeline is:

  • [=] gave warning: implicit capture of ‘this’ via ‘[=]’ is deprecated in C++20 [-Wdeprecated]
  • [=, this] done in !1811 (merged) to solve the warning in the first point outputs either warning: explicit by-copy capture of ‘this’ redundant with by-copy capture default [enabled by default] or warning: explicit capture of 'this' with a capture default of '=' is a C++20 extension. This is not the desired effect. I wanted to capture everything by copy and only this by reference, but this is not how you do it, you should have & before the variables in the part after the comma, but &this results in compiler errors.
  • [&] in this MR should solve all problems and is safe because the only variable that is used inside the lambda is state_ which is passed by reference because it's owner this is also passed by reference even if we had used [=]. So the behavior doesn't change from the first point.

Here are the full warnings:

  CXX      libkea_dhcpsrv_la-alloc_engine.lo
In file included from ../../../src/lib/dhcpsrv/alloc_engine.h:26:0,
                 from alloc_engine.cc:14:
../../../src/lib/util/readwrite_mutex.h: In member function ‘void isc::util::ReadWriteMutex::writeLock()’:
../../../src/lib/util/readwrite_mutex.h:57:29: warning: explicit by-copy capture of ‘this’ redundant with by-copy capture default [enabled by default]
         gate1_.wait(lk, [=, this]() { return (!writeEntered()); });
                             ^
../../../src/lib/util/readwrite_mutex.h:60:29: warning: explicit by-copy capture of ‘this’ redundant with by-copy capture default [enabled by default]
         gate2_.wait(lk, [=, this]() { return (readers() == 0); });
                             ^
../../../src/lib/util/readwrite_mutex.h: In member function ‘void isc::util::ReadWriteMutex::readLock()’:
../../../src/lib/util/readwrite_mutex.h:77:29: warning: explicit by-copy capture of ‘this’ redundant with by-copy capture default [enabled by default]
         gate1_.wait(lk, [=, this]() { return (state_ < MAX_READERS); });

or

../../../src/lib/util/readwrite_mutex.h:57:29: warning: explicit capture of 'this' with a capture default of '=' is a C++20 extension [-Wc++20-extensions]
        gate1_.wait(lk, [=, this]() { return (!writeEntered()); });
                            ^
../../../src/lib/util/readwrite_mutex.h:60:29: warning: explicit capture of 'this' with a capture default of '=' is a C++20 extension [-Wc++20-extensions]
        gate2_.wait(lk, [=, this]() { return (readers() == 0); });
                            ^
../../../src/lib/util/readwrite_mutex.h:77:29: warning: explicit capture of 'this' with a capture default of '=' is a C++20 extension [-Wc++20-extensions]
        gate1_.wait(lk, [=, this]() { return (state_ < MAX_READERS); });
Edited by Andrei Pavel

Merge request reports