operator==, !=, <, <=, >, >=, <=> (std::shared_ptr)
| 
           定义于头文件
            
            
             <memory>
            
            | ||
| 
           比较两个
            
            shared_ptr
           对象。 | ||
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              ==
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (1) | (C++11 起) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              !
             
             
              =
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (2) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              <
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (3) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              >
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (4) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              <=
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (5) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              bool
             
             operator
             
              >=
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (6) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T,
            
             class
            
            U
            
             >
            
             
             
              
               std::
               
                strong_ordering
               
              
             
             operator
             
              <=>
             
             
              (
             
             
              const
             
             
              
               std::
               
                shared_ptr
               
              
             
             
              <
             
             T
             
              >
             
             
              &
             
             lhs,
              | (7) | (C++20 起) | 
| 
           比较
            
            shared_ptr
           与空指针。 | ||
| 
           
            
             template
            
            
             <
            
            
             class
            
            T
            
             >
            
             bool operator == ( const std:: shared_ptr < T > & lhs, std:: nullptr_t ) noexcept ; | (8) | (C++11 起) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T
            
             >
            
             bool operator == ( std:: nullptr_t , const std:: shared_ptr < T > & rhs ) noexcept ; | (9) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T
            
             >
            
             bool operator ! = ( const std:: shared_ptr < T > & lhs, std:: nullptr_t ) noexcept ; | (10) | (C++11 起) (C++20 前) | 
| 
           
            
             template
            
            
             <
            
            
             class
            
            T
            
             >
            
             bool operator ! = ( std:: nullptr_t , const std:: shared_ptr < T > & rhs ) noexcept <span class="sy | 
       比较两个
       
        shared_ptr<T>
       
       对象,或比较
       
        shared_ptr<T>
       
       与空指针。
      
       请注意,
       
        shared_ptr
       
       的比较运算符仅比较指针值;所指向的实际对象
       
        不会
       
       被比较。为
       
        shared_ptr
       
       定义
       
        operator<
       
       使得
       
        shared_ptr
       
       可以作为关联容器的键使用,例如
       
        
         std::map
        
       
       和
       
        
         std::set
        
       
       。
      
| 
           | (since C++20) | 
| 目录 | 
参数
| lhs | - | 待比较的左侧 
          shared_ptr
          | 
| rhs | - | 待比较的右侧 
          shared_ptr
          | 
返回值
注释
在所有情况下,比较的是存储的指针(即通过 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' // 自 C++20 起 // p1 和 p2 指向不同的内存位置,因此 p1 != p2 << "(p1 == p2) : " << (p1 == p2) << '\n' << "(p1 < p2) : " << (p1 < p2) << '\n' << "(p1 <=> p2) < 0 : " << ((p1 <=> p2) < 0) << '\n' // 自 C++20 起 << "(p1 <=> p2) == 0 : " << ((p1 <=> p2) == 0) << '\n'; // 自 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)
         存在格式错误 | 定义已修复 | 
另请参阅
| 返回存储的指针 (公开成员函数) |