Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |memory‎ |unique ptr
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
       
      Defined in header<memory>
      template<class T1,class D1,class T2,class D2>
      bool operator==(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (1)(since C++11)
      (constexpr since C++23)
      template<class T1,class D1,class T2,class D2>
      bool operator!=(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (2)(since C++11)
      (until C++20)
      template<class T1,class D1,class T2,class D2>
      bool operator<(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (3)(since C++11)
      template<class T1,class D1,class T2,class D2>
      bool operator<=(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (4)(since C++11)
      template<class T1,class D1,class T2,class D2>
      bool operator>(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (5)(since C++11)
      template<class T1,class D1,class T2,class D2>
      bool operator>=(const unique_ptr<T1, D1>& x,const unique_ptr<T2, D2>& y);
      (6)(since C++11)
      template<class T1,class D1,class T2,class D2>

          requiresstd::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);
      (7)(since C++20)
      template<class T,class D>
      bool operator==(const unique_ptr<T, D>& x,std::nullptr_t)noexcept;
      (8)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator==(std::nullptr_t,const unique_ptr<T, D>& x)noexcept;
      (9)(since C++11)
      (until C++20)
      template<class T,class D>
      bool operator!=(const unique_ptr<T, D>& x,std::nullptr_t)noexcept;
      (10)(since C++11)
      (until C++20)
      template<class T,class D>
      bool operator!=(std::nullptr_t,const unique_ptr<T, D>& x)noexcept;
      (11)(since C++11)
      (until C++20)
      template<class T,class D>
      bool operator<(const unique_ptr<T, D>& x,std::nullptr_t);
      (12)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator<(std::nullptr_t,const unique_ptr<T, D>& y);
      (13)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator<=(const unique_ptr<T, D>& x,std::nullptr_t);
      (14)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator<=(std::nullptr_t,const unique_ptr<T, D>& y);
      (15)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator>(const unique_ptr<T, D>& x,std::nullptr_t);
      (16)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator>(std::nullptr_t,const unique_ptr<T, D>& y);
      (17)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator>=(const unique_ptr<T, D>& x,std::nullptr_t);
      (18)(since C++11)
      (constexpr since C++23)
      template<class T,class D>
      bool operator>=(std::nullptr_t,const unique_ptr<T, D>& y);
      (19)(since C++11)
      (constexpr since C++23)
      template<class T,class D>

          requiresstd::three_way_comparable<typename unique_ptr<T, D>::pointer>
      std::compare_three_way_result_t<typename unique_ptr<T, D>::pointer>

          operator<=>(const unique_ptr<T, D>& x,std::nullptr_t);
      (20)(since C++20)
      (constexpr since C++23)

      Compares the pointer values of twounique_ptrs, or aunique_ptr andnullptr.

      1-7) Compares twounique_ptrs.
      8-20) Compares aunique_ptr andnullptr.

      The!= operator issynthesized fromoperator==.

      (since C++20)

      Contents

      [edit]Parameters

      x, y -unique_ptrs to compare

      [edit]Return value

      1)x.get()== y.get()
      2)x.get()!= y.get()
      3)std::less<CT>()(x.get(), y.get()), whereCT isstd::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
      12)std::less<unique_ptr<T,D>::pointer>()(x.get(), nullptr)
      13)std::less<unique_ptr<T,D>::pointer>()(nullptr, y.get())
      14)!(nullptr< x)
      15)!(y< nullptr)
      16)nullptr< x
      17)y< nullptr
      18)!(x< nullptr)
      19)!(nullptr< y)
      20)std::compare_three_way{}(x.get(),static_cast<typename unique_ptr<T, D>::pointer>(nullptr))

      [edit]Example

      Run this code
      #include <iostream>#include <memory> int main(){std::unique_ptr<int> p1(newint(42));std::unique_ptr<int> p2(newint(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 output:

      (p1 == p1)       : true(p1 <=> p1) == 0 : true(p1 == p2)       : false(p1 < p2)        : true(p1 <=> p2) < 0  : true(p1 <=> p2) == 0 : false

      [edit]Defect reports

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

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

      [edit]See also

      returns a pointer to the managed object
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/unique_ptr/operator_cmp&oldid=160319"

      [8]ページ先頭

      ©2009-2025 Movatter.jp