在线文档教程
C++
应用 | Utilities

operators (std::unique_ptr)

运算符=,%21=,<,<=,>,>=%28std::UNIQUE[医]PTR%29

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

比较两个指针的值。unique_ptrs,或aunique_ptrnullptr...

1-6%29比较两unique_ptr

7-18%29比较aunique_ptrnullptr...

参数

x, y-unique_ptrs to compare

返回值

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

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

3%29std::less<CT>()(x.get(), y.get()),在哪里CT是std::common_type<unique_ptr<T1, D1>::pointer, unique_ptr<T2, D2>::pointer>::type...

4%29!(y < x)...

5%29y < x...

6%29!(x < y)...

7-8%29!x...

9-10%29(bool)x...

11%29std::less<unique_ptr<T,D>::pointer>()(x.get(), nullptr)...

12%29std::less<unique_ptr<T,D>::pointer>()(nullptr, y.get())...

13%29nullptr < x...

14%29y < nullptr...

15%29!(nullptr < x)...

16%29!(y < nullptr)...

17%29!(x < nullptr)...

18%29!(nullptr < y)...

二次

#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 and p2 point to different memory locations, so p1 != p2 std::cout << "p1 == p2: " << (p1 == p2) << '\n'; }

二次

产出:

二次

p1 == p1: 1 p1 == p2: 0

二次

另见

getreturns a pointer to the managed object (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Memory/UNIQUE[医]PTR/算子[医]CMP