function template
<memory>

std::relational operators (shared_ptr)

(1)
template <class T, class U>
  bool operator== (const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
template <class T> bool operator== (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator== (nullptr_t, const shared_ptr<T>& rhs) noexcept;
(2)
template <class T> bool operator!= (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator!= (nullptr_t, const shared_ptr<T>& rhs) noexcept;
(3)
template <class T, class U>
  bool operator<  (const shared_ptr<T>& lhs, const shared_ptr<U>& rhs) noexcept;
template <class T> bool operator<  (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator<  (nullptr_t, const shared_ptr<T>& rhs) noexcept;
(4)
template <class T> bool operator<= (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator<= (nullptr_t, const shared_ptr<T>& rhs) noexcept;
(5)
template <class T> bool operator>  (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator>  (nullptr_t, const shared_ptr<T>& rhs) noexcept;
(6)
template <class T> bool operator>= (const shared_ptr<T>& lhs, nullptr_t) noexcept;
template <class T> bool operator>= (nullptr_t, const shared_ptr<T>& rhs) noexcept;
Relational operators ==, !=, <, <=, >, >=
Performs the appropriate relational comparison operation between the shared_ptr objects lhs and rhs, or between a shared_ptr and a nullptr.

The comparison compares directly the stored pointers (i.e., the value the objects dereference to, and not their owned pointer (i.e., the managed objects that are deleted on destruction), which may not be the same in alias shared_ptr objects (alias-constructed objects and their copies). To perform owner-based comparisons, see owner_less instead.

Only the operators needed to use the type directly in the standard associative containers are provided: == and <, along with those that compare against a null pointer (any null pointer compares less than any non-null pointer, and any two null pointers compare equal).

Parameters

lhs, rhs
shared_ptr objects (to the left- and right-hand side of the operator, respectively).

Return Value

true if the condition holds, and false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// shared_ptr relational operators
#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> a,b,c,d;

  a = std::make_shared<int> (10);
  b = std::make_shared<int> (10);
  c = b;

  std::cout << "comparisons:\n" << std::boolalpha;

  std::cout << "a == b: " << (a==b) << '\n';
  std::cout << "b == c: " << (b==c) << '\n';
  std::cout << "c == d: " << (c==d) << '\n';

  std::cout << "a != nullptr: " << (a!=nullptr) << '\n';
  std::cout << "b != nullptr: " << (b!=nullptr) << '\n';
  std::cout << "c != nullptr: " << (c!=nullptr) << '\n';
  std::cout << "d != nullptr: " << (d!=nullptr) << '\n';

  return 0;
}


Output:
comparisons:
a == b: false
b == c: true
c == d: false
a != nullptr: true
b != nullptr: true
c != nullptr: true
d != nullptr: false

See also