c++11 智能指针 (std::unique_ptr)(六)

定义于头文件 <memory>
template< class T, class Deleter = std::default_delete<T>>
 class unique_ptr; (1) (C++11 起) 
template < class T,class Deleter>
class unique_ptr<T[], Deleter>; (2) (C++11 起) 

与另一个 unique_ptr 或 nullptr 进行比较

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

template<class T1, class D1, class T2, class D2>
bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);

(1)(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)(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)(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)(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)(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)(C++11 起)

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

(7)(C++11 起)

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

(8)(C++11 起)

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

(9)(C++11 起)

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

(10)(C++11 起)

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

(11)(C++11 起)

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

(12)(C++11 起)

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

(13)(C++11 起)

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

(14)(C++11 起)

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

(15)(C++11 起)

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

(16)(C++11 起)

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

(17)(C++11 起)

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

(18)(C++11 起)

比较二个 unique_ptrunique_ptr 与 nullptr 的指针值。

1-6) 比较二个 unique_ptr

7-18) 比较 unique_ptr 与 nullptr 。

参数

x, y-要比较的 unique_ptr

返回值

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

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

3) std::less<CT>()(x.get(), y.get()) ,其中 CT 是 std::common_type<unique_ptr<T1, D1>::pointer, unique_ptr<T2, D2>::pointer>::type

4) !(y < x)

5) y < x

6) !(x < y)

7-8) !x

9-10) (bool)x

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

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

13) !(nullptr < x)

14) !(y < nullptr)

15) nullptr < x

16) y < nullptr

17) !(x < nullptr)

18) !(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 与 p2 指向不同内存位置,故 p1 != p2
    std::cout << "p1 == p2: " << (p1 == p2) << '\n';
}

 输出 

特化 std::swap 算法

std::swap(std::unique_ptr)
template< class T, class Deleter >
void swap( unique_ptr<T,Deleter>& lhs, 
unique_ptr<T,Deleter>& rhs ) noexcept;(C++11 起) 

为 std::unique_ptr 特化 std::swap 算法。交换 lhsrhs 的指针。调用 lhs.swap(rhs) 。

此重载仅若 std::is_swappable<D>::value 为 true 才参与重载决议。

(C++17 起)

参数

lhs, rhs-要交换内容的智能指针

返回值

(无)

复杂度

常数

std::unique_ptr 的散列支持

std::hash <std::unique_ptr>
template<class T, class Deleter> struct hash<unique_ptr<T, Deleter>>;(C++11 起) 

std::hash 对 std::unique_ptr<T, Deleter> 的模板特化允许用户获得 std::unique_ptr<T, Deleter> 类型对象的哈希值。

若启用 std::hash<typename std::unique_ptr<T,D>::pointer> ,则启用特化 std::hash<std::unique_ptr<T,D>> (见 std::hash ),否则禁用它。

(C++17 起)

启用时, (C++17 起)对于给定的 std::unique_ptr<T, D> p ,此特化确保 std::hash<std::unique_ptr<T, D>>()(p) == std::hash<typename std::unique_ptr<T, D>::pointer>()(p.get()) 。

此特化的成员函数不保证为 noexcept ,因为指针可能是缀饰指针且其哈希可能抛出。

调用示例

#include <iostream>
#include <memory>
#include <functional>

struct Foo
{
    Foo()
    {
        std::cout << "Foo...\n";
    }
    ~Foo()
    {
        std::cout << "~Foo...\n\n";
    }
};

int main()
{
    Foo* foo = new Foo();
    std::unique_ptr<Foo> up(foo);

    std::cout << "hash(up):  " << std::hash<std::unique_ptr<Foo>>()(up) << '\n';
    std::cout << "hash(foo): " << std::hash<Foo*>()(foo) << '\n';
}

输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值