public member function
<memory>

std::unique_ptr::operator bool

explicit operator bool() const noexcept;
Check if not empty
Returns whether an object is currently managed by the unique_ptr (i.e., whether the unique_ptr is not empty).

The function returns true whenever the stored pointer is not a null pointer, thus returning the same as:
1
get()!=

Parameters

none

Return value

false if the unique_ptr is empty.
true, otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// example of unique_ptr::operator bool
#include <iostream>
#include <memory>


int main () {
  std::unique_ptr<int> foo;
  std::unique_ptr<int> bar (new int(12));

  if (foo) std::cout << "foo points to " << *foo << '\n';
  else std::cout << "foo is empty\n";

  if (bar) std::cout << "bar points to " << *bar << '\n';
  else std::cout << "bar is empty\n";

  return 0;
}

Output:
foo is empty
bar points to 12


See also