warning C4355: 'this' : used in base member initializer list
Visual C++ 2010 から取得します。
ハンドルを保持しているクラスがあり、クラスの ctor が失敗した場合でもハンドルを自動的に閉じたい(そのため、dtor は呼び出されません)。ただし、ハンドル ラッピング クラス全体をわざわざ作成したくはありません。むしろ、それをスマート ポインターに保持したいと考えています。そして、私はこれを書いた:
foo.h
~~~~~
class Foo
{
...
Log &_log;
std::unique_ptr<void, std::function<void (void *)>> _handle;
...
}
foo.cpp
~~~~~~~
#include <windows.h>
Foo::Foo(Log &lg, ...) : _log(lg), ... _handle(nullptr, [&](void *h){ if (h) { if (!CloseHandle(h)) LOG(_log, "Could not close port: " << LastWinErr()); h = nullptr; } })
{
HANDLE h(CreateFile( ...
if (h == ...
_handle.reset(h);
... // Bunch of other stuff that could potentially throw
}
クロージャーの前に、 _handle を のようなもので初期化していまし_handle(nullptr, bind(PortDeleter, placeholders::_1, ref(_log)))
たが、それには別の定義が必要です。
私の質問: 警告は、この特定のインスタンスに対する懸念事項ですか? いずれにせよ、詳細な理由は何ですか?それを回避する簡単な方法はありますか?