Skip to main content

operator==,!=,<,<=,>,>=,<=>(std::unique_ptr)

Declaration

// 1)
template< class T1, class D1, class T2, class D2 >
constexpr bool operator==( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 3)
template< class T1, class D1, class T2, class D2 >
bool operator<( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 4)
template< class T1, class D1, class T2, class D2 >
bool operator<=( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y )
// 5)
template< class T1, class D1, class T2, class D2 >
bool operator>( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 6)
template< class T1, class D1, class T2, class D2 >
bool operator>=( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 7)
template< class T1, class D1, class T2, class D2 >
requires std::three_way_comparable_with<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
std::compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
operator<=>( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 8)
template< class T, class D >
constexpr bool operator==( const unique_ptr<T, D>& x, std::nullptr_t) noexcept;
// 12)
template< class T, class D >
constexpr bool operator<( const unique_ptr<T, D>& x, std::nullptr_t );
// 13)
template< class T, class D >
constexpr bool operator<(std::nullptr_t, const unique_ptr<T, D>& y );
// 14)
template< class T, class D >
constexpr bool operator<=( const unique_ptr<T, D>& x, std::nullptr_t );
// 15)
template< class T, class D >
constexpr bool operator<=(std::nullptr_t, const unique_ptr<T, D>& y );
// 16)
template< class T, class D >
constexpr bool operator>( const unique_ptr<T, D>& x, std::nullptr_t );
// 17)
template< class T, class D >
constexpr bool operator>(std::nullptr_t, const unique_ptr<T, D>& y );
// 18)
template< class T, class D >
constexpr bool operator>=( const unique_ptr<T, D>& x, std::nullptr_t );
// 19)
template< class T, class D >
constexpr bool operator>=(std::nullptr_t, const unique_ptr<T, D>& y );
// 20)
template< class T, class D >
requires std::three_way_comparable<typename unique_ptr<T, D>::pointer>
constexpr std::compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
operator<=>( const unique_ptr<T, D>& x, std::nullptr_t );

Compares the pointer values of two unique_ptrs, or a unique_ptr and nullptr.

1-7)
Compares two unique_ptrs
8-20)
Compares a unique_ptr and nullptr.

The != operator is synthesized from operator==.(since C++20)

Parameters

x, y - unique_ptrs to compare

Return value

  1. x.get() == y.get()
  2. x.get() != y.get()
  3. std::less<CT>()(x.get(), y.get()),
    where CT is std::common_type<unique_ptr<T1, D1>::pointer, unique_ptr<T2, D2>::pointer>::type
  4. !(y < x)
  5. y < x
  6. !(x < y)
  7. std::compare_three_way{}(x.get(), y.get())
    8-9) !x
    10-11) (bool)x
  8. std::less<unique_ptr<T,D>::pointer>()(x.get(), nullptr)<br/>
  9. std::less<unique_ptr<T,D>::pointer>()(nullptr, y.get())<br/>
  10. !(nullptr < x)
  11. !(y < nullptr)
  12. nullptr < x
  13. y < nullptr
  14. !(x < nullptr)
  15. !(nullptr < y)
  16. std::compare_three_way{}(x.get(), static_cast<typename unique_ptr<T, D>::pointer>(nullptr))

Example

#include <iostream>
#include <memory>

int main()
{
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2(new int(42));

std::cout << std::boolalpha
<< "(p1 == p1) : "
<< (p1 == p1) << '\n'
<< "(p1 <=> p1) == 0 : "
<< ((p1 <=> p1) == 0) << '\n' // Since C++20

// p1 and p2 point to different memory locations, so p1 != p2
<< "(p1 == p2) : "
<< (p1 == p2) << '\n'
<< "(p1 < p2) : "
<< (p1 < p2) << '\n'
<< "(p1 <=> p2) < 0 : "
<< ((p1 <=> p2) < 0) << '\n' // Since C++20
<< "(p1 <=> p2) == 0 : "
<< ((p1 <=> p2) == 0) << '\n'; // Since C++20
}
Possible Result
(p1 == p1)       : true
(p1 <=> p1) == 0 : true
(p1 == p2) : false
(p1 < p2) : true
(p1 <=> p2) < 0 : true
(p1 <=> p2) == 0 : false

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 3426C++20uoperator<=>(unique_ptr, nullptr_t) was ill-formedconstraints and definition fixed

operator==,!=,<,<=,>,>=,<=>(std::unique_ptr)

Declaration

// 1)
template< class T1, class D1, class T2, class D2 >
constexpr bool operator==( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 3)
template< class T1, class D1, class T2, class D2 >
bool operator<( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 4)
template< class T1, class D1, class T2, class D2 >
bool operator<=( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y )
// 5)
template< class T1, class D1, class T2, class D2 >
bool operator>( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 6)
template< class T1, class D1, class T2, class D2 >
bool operator>=( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 7)
template< class T1, class D1, class T2, class D2 >
requires std::three_way_comparable_with<
typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
std::compare_three_way_result_t<typename unique_ptr<T1, D1>::pointer,
typename unique_ptr<T2, D2>::pointer>
operator<=>( const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y );
// 8)
template< class T, class D >
constexpr bool operator==( const unique_ptr<T, D>& x, std::nullptr_t) noexcept;
// 12)
template< class T, class D >
constexpr bool operator<( const unique_ptr<T, D>& x, std::nullptr_t );
// 13)
template< class T, class D >
constexpr bool operator<(std::nullptr_t, const unique_ptr<T, D>& y );
// 14)
template< class T, class D >
constexpr bool operator<=( const unique_ptr<T, D>& x, std::nullptr_t );
// 15)
template< class T, class D >
constexpr bool operator<=(std::nullptr_t, const unique_ptr<T, D>& y );
// 16)
template< class T, class D >
constexpr bool operator>( const unique_ptr<T, D>& x, std::nullptr_t );
// 17)
template< class T, class D >
constexpr bool operator>(std::nullptr_t, const unique_ptr<T, D>& y );
// 18)
template< class T, class D >
constexpr bool operator>=( const unique_ptr<T, D>& x, std::nullptr_t );
// 19)
template< class T, class D >
constexpr bool operator>=(std::nullptr_t, const unique_ptr<T, D>& y );
// 20)
template< class T, class D >
requires std::three_way_comparable<typename unique_ptr<T, D>::pointer>
constexpr std::compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
operator<=>( const unique_ptr<T, D>& x, std::nullptr_t );

Compares the pointer values of two unique_ptrs, or a unique_ptr and nullptr.

1-7)
Compares two unique_ptrs
8-20)
Compares a unique_ptr and nullptr.

The != operator is synthesized from operator==.(since C++20)

Parameters

x, y - unique_ptrs to compare

Return value

  1. x.get() == y.get()
  2. x.get() != y.get()
  3. std::less<CT>()(x.get(), y.get()),
    where CT is std::common_type<unique_ptr<T1, D1>::pointer, unique_ptr<T2, D2>::pointer>::type
  4. !(y < x)
  5. y < x
  6. !(x < y)
  7. std::compare_three_way{}(x.get(), y.get())
    8-9) !x
    10-11) (bool)x
  8. std::less<unique_ptr<T,D>::pointer>()(x.get(), nullptr)<br/>
  9. std::less<unique_ptr<T,D>::pointer>()(nullptr, y.get())<br/>
  10. !(nullptr < x)
  11. !(y < nullptr)
  12. nullptr < x
  13. y < nullptr
  14. !(x < nullptr)
  15. !(nullptr < y)
  16. std::compare_three_way{}(x.get(), static_cast<typename unique_ptr<T, D>::pointer>(nullptr))

Example

#include <iostream>
#include <memory>

int main()
{
std::unique_ptr<int> p1(new int(42));
std::unique_ptr<int> p2(new int(42));

std::cout << std::boolalpha
<< "(p1 == p1) : "
<< (p1 == p1) << '\n'
<< "(p1 <=> p1) == 0 : "
<< ((p1 <=> p1) == 0) << '\n' // Since C++20

// p1 and p2 point to different memory locations, so p1 != p2
<< "(p1 == p2) : "
<< (p1 == p2) << '\n'
<< "(p1 < p2) : "
<< (p1 < p2) << '\n'
<< "(p1 <=> p2) < 0 : "
<< ((p1 <=> p2) < 0) << '\n' // Since C++20
<< "(p1 <=> p2) == 0 : "
<< ((p1 <=> p2) == 0) << '\n'; // Since C++20
}
Possible Result
(p1 == p1)       : true
(p1 <=> p1) == 0 : true
(p1 == p2) : false
(p1 < p2) : true
(p1 <=> p2) < 0 : true
(p1 <=> p2) == 0 : false

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 3426C++20uoperator<=>(unique_ptr, nullptr_t) was ill-formedconstraints and definition fixed