如何避免VS中unique_ptr检查的性能警告?

Kit*_*sto 5 c++ unique-ptr visual-c++

这段代码:

    unique_ptr<int> a;
    if (a) {
        cout << "ASSIGNED" << endl;
    }
Run Code Online (Sandbox Code Playgroud)

甚至这段代码:

    unique_ptr<int> a;
    if (static_cast<bool>(a)) {
        cout << "ASSIGNED" << endl;
    }
Run Code Online (Sandbox Code Playgroud)

导致此警告:

warning C4800: 'void (__cdecl *)(std::_Bool_struct<_Ty> &)' : forcing value to bool 'true' or 'false' (performance warning)
with
[
    _Ty=std::unique_ptr<int>
]
Run Code Online (Sandbox Code Playgroud)

在警告级别3的Visual Studio 2012中.在第一次评论之后,我发现只有在打开公共语言运行时支持/ clr时才会发生这种情况.我应该如何避免它?

if (a.get() != nullptr)
Run Code Online (Sandbox Code Playgroud)

应该工作,但我认为这不是unique_ptr的设计方式,是吗?

Jar*_*d42 2

您可以直接使用

if (a != nullptr)
Run Code Online (Sandbox Code Playgroud)