std::unique_ptr::operator bool

From cppreference.com
< cpp‎ | memory‎ | unique ptr
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
 
 
explicit operator bool() const;
(since C++11)

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

Contents

[edit] Parameters

(none)

[edit] Return value

true if *this owns an object, false otherwise.

[edit] Exceptions

noexcept specification:  
noexcept
  

[edit] 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

[edit] See also

returns a pointer to the managed object
(public member function)