operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
定义于头文件 <memory>
。
- C++20
- C++11
比较两个 shared_ptr 对象
// 1)
template < class T, class U >
bool operator==( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 7)
template< class T, class U >
std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
比较 shared_ptr 和空指针
// 8)
template< class T >
bool operator==( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 20)
template< class T >
std::strong_ordering operator<=>( const std::shared_ptr<T>& lhs,
std::nullptr_t ) noexcept;
比较两个 shared_ptr 对象
// 1)
template < class T, class U >
bool operator==( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 2)
template< class T, class U >
bool operator!=( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 3)
template< class T, class U >
bool operator<( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 4)
template< class T, class U >
bool operator>( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 5)
template< class T, class U >
bool operator<=( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
// 6)
template< class T, class U >
bool operator>=( const std::shared_ptr<T>& lhs,
const std::shared_ptr<U>& rhs ) noexcept;
比较 shared_ptr 和空指针
//8)
template< class T >
bool operator==( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 9)
template< class T >
bool operator==( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
// 10)
template< class T >
bool operator!=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 11)
template< class T >
bool operator!=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
// 12)
template< class T >
bool operator<( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 13)
template< class T >
bool operator<( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
// 14)
template< class T >
bool operator>( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 15)
template< class T >
bool operator>( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
// 16)
template< class T >
bool operator<=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 17)
template< class T >
bool operator<=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
// 18)
template< class T >
bool operator>=( const std::shared_ptr<T>& lhs, std::nullptr_t ) noexcept;
// 19)
template< class T >
bool operator>=( std::nullptr_t, const std::shared_ptr<T>& rhs ) noexcept;
比较两个 shared_ptr<T>
对象,或比较 shared_ptr<T>
与 null
指针。
请注意,shared_ptr
的比较运算符仅比较指针值;不比较实际指向的对象。为 shared_ptr
定义 operator<
允许 shared_ptr
作为关联容器(如 std::map
和 std::set
)的键。
(自 C++20 起)<
, <=
, >
, >=
, 和 !=
运算符分别从 operator<=>
和 operator==
合成而来。
参数
lhs
- 要比较的左侧 shared_ptr
rhs
- 要比较的右侧 shared_ptr
返回值
lhs.get() == rhs.get()
!(lhs == rhs)
std::less<V>()(lhs.get(), rhs.get())
,其中V
是std::shared_ptr<T>::element_type*
和std::shared_ptr<U>::element_type*
的复合指针类型rhs < lhs
!(rhs < lhs)
!(lhs < rhs)
std::compare_three_way{}(x.get(), y.get())
!lhs
!rhs
(bool)lhs
(bool)rhs
std::less<std::shared_ptr<T>::element_type*>()(lhs.get(), nullptr)
std::less<std::shared_ptr<T>::element_type*>()(nullptr, rhs.get())
nullptr < lhs
rhs < nullptr
!(nullptr < lhs)
!(rhs < nullptr)
!(lhs < nullptr)
!(nullptr < rhs)
std::compare_three_way{}(x.get(), static_cast<std::shared_ptr<T>::element_type*>(nullptr))
备注
在所有情况下,比较的是存储的指针(由 get()
返回的指针),而不是托管的指针(当 use_count
变为零时传递给删除器的指针)。使用别名构造函数创建的 shared_ptr
中,这两个指针可能不同。
示例
#include <iostream>
#include <memory>
int main()
{
std::shared_ptr<int> p1(new int(42));
std::shared_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
}
(p1 == p1) : true
(p1 <=> p1) == 0 : true
(p1 == p2) : false
(p1 < p2) : true
(p1 <=> p2) < 0 : true
(p1 <=> p2) == 0 : false
缺陷报告
以下改变行为的缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 3427 | C++20 | operator<=>(shared_ptr, nullptr_t) 是 ill-formed(格式不正确) | 定义已修复 |