Namespaces
Variants
Views
Actions

std::unique_ptr::operator bool

From cppreference.com
< cpp‎ | memory‎ | unique ptr
Revision as of 03:09, 7 January 2013 by Cubbi (Talk | contribs)

 
 
 
 
 
explicit operator bool() const;
(since C++11)

Checks whether *this owns an object, i.e. whether get() == nullptr.

Contents

Parameters

(none)

Return value

true if *this owns an object, false otherwise.

Exceptions

noexcept specification:  
noexcept
  

Example

#include <iostream>
#include <memory>
 
int main()
{
    std::unique_ptr<int> ptr(new int(42));
 
    if (ptr) std::cout << "before reset, ptr is: " << *ptr << '\n';
    ptr.reset();
    if (ptr) std::cout << "after reset, ptr is: " << *ptr << '\n';
}

Output:

before reset, ptr is: 42

See also

Template:cpp/memory/unique ptr/dcl list get