Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid performance warning for unique_ptr check in VS?

This code:

    unique_ptr<int> a;
    if (a) {
        cout << "ASSIGNED" << endl;
    }

and even this code:

    unique_ptr<int> a;
    if (static_cast<bool>(a)) {
        cout << "ASSIGNED" << endl;
    }

cause this warning:

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

in Visual Studio 2012 on warning level 3. After the first comments I found out that it only happens if common language runtime support /clr is switched on. How should I avoid it?

if (a.get() != nullptr)

should work, but I think that is not how unique_ptr was designed, was it?

like image 788
Kit Fisto Avatar asked Nov 01 '22 07:11

Kit Fisto


1 Answers

You may use directly

if (a != nullptr)
like image 151
Jarod42 Avatar answered Nov 15 '22 04:11

Jarod42