operator==,!=,<,<=,>,>=,<=>(std::unique_ptr)_C++中文网

    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);

(7) (C++20 起)

template <class T, class D>
bool operator==(const unique_ptr<T, D>& x, std::nullptr_t) noexcept;

(8) (C++11 起)

template <class T, class D>
bool operator==(std::nullptr_t, const unique_ptr<T, D>& x) noexcept;

(9) (C++11 起)
(C++20 前)

template <class T, class D>
bool operator!=(const unique_ptr<T, D>& x, std::nullptr_t) noexcept;

(10) (C++11 起)
(C++20 前)

template <class T, class D>
bool operator!=(std::nullptr_t, const unique_ptr<T, D>& x) noexcept;

(11) (C++11 起)
(C++20 前)

template <class T, class D>
bool operator<(const unique_ptr<T, D>& x, std::nullptr_t);

(12) (C++11 起)

template <class T, class D>
bool operator<(std::nullptr_t, const unique_ptr<T, D>& y);

(13) (C++11 起)

template <class T, class D>
bool operator<=(const unique_ptr<T, D>& x, std::nullptr_t);

(14) (C++11 起)

template <class T, class D>
bool operator<=(std::nullptr_t, const unique_ptr<T, D>& y);

(15) (C++11 起)

template <class T, class D>
bool operator>(const unique_ptr<T, D>& x, std::nullptr_t);

(16) (C++11 起)

template <class T, class D>
bool operator>(std::nullptr_t, const unique_ptr<T, D>& y);

(17) (C++11 起)

template <class T, class D>
bool operator>=(const unique_ptr<T, D>& x, std::nullptr_t);

(18) (C++11 起)

template <class T, class D>
bool operator>=(std::nullptr_t, const unique_ptr<T, D>& y);

(19) (C++11 起)

template<class T, class D>

    requires std::three_way_comparable_with<
        typename unique_ptr<T, D>::pointer,
        std::nullptr_t>
std::compare_three_way_result_t<typename unique_ptr<T, D>::pointer,
                                std::nullptr_t>

    operator<=>(const unique_ptr<T, D>& x, std::nullptr_t);

(20) (C++20 起)

比较二个 unique_ptrunique_ptrnullptr 的指针值。

1-7) 比较二个 unique_ptr

8-20) 比较 unique_ptrnullptr

参数

返回值

1) x.get() == y.get()

2) x.get() != y.get()

3) std::less<CT>()(x.get(), y.get()) ,其中 CTstd::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(), nullptr)

示例

#include <iostream>
#include <memory>
 
int main() 
{
    std::unique_ptr<int> p1(new int(42));
    std::unique_ptr<int> p2(new int(42));
 
    std::cout << "p1 == p1: " << (p1 == p1) << '\n';
 
    // p1 与 p2 指向不同内存位置,故 p1 != p2
    std::cout << "p1 == p2: " << (p1 == p2) << '\n';
}

输出:

参阅

返回指向被管理对象的指针
(公开成员函数)