c++11 智能指针 (std::shared_ptr)(四)

  定义于头文件 <memory>
template< class T > class shared_ptr;     (C++11 起) 

返回指定类型中的删除器,若其拥有

std::get_deleter
template< class Deleter, class T >
Deleter* get_deleter( const std::shared_ptr<T>& p ) noexcept;  (C++11 起) 

访问 p 的删除器。若共享指针 p 占有无 cv 限定 Deleter 类型的删除器(例如,若它以接收删除器为参数的构造函数之一创建),则返回指向删除器的指针。否则,返回空指针。

参数

p-需要访问其删除器的共享指针

返回值

指向被占有删除器的指针或 nullptr 。只要至少还有一个 shared_ptr 实例占有返回的指针,它就合法。

注意

返回的指针可能比最后一个 shared_ptr 的生存期更持久,例如,还剩下 std::weak_ptr 时且实现在销毁整个控制块前不销毁删除器。

调用示例

#include <iostream>
#include <memory>

struct Foo
{
    int i;
};
void foo_deleter(Foo * p)
{
    std::cout << "foo_deleter called!\n";
    delete p;
}

int main()
{
    std::shared_ptr<int> aptr;

    {
        // 创建拥有一个 Foo 和删除器的 shared_ptr
        auto foo_p = new Foo;
        std::shared_ptr<Foo> r(foo_p, foo_deleter);
        aptr = std::shared_ptr<int>(r, &r->i); // 别名使用构造函数
        // aptr 现在指向 int ,但管理整个 Foo
    } // r 被销毁(不调用删除器)

    // 获得指向删除器的指针:
    if (auto del_p = std::get_deleter<void(*)(Foo*)>(aptr))
    {
        std::cout << "shared_ptr<int> owns a deleter\n";
        if (*del_p == foo_deleter)
        {
            std::cout << "...and it equals &foo_deleter\n";
        }
    }
    else
    {
        std::cout << "The deleter of shared_ptr<int> is null!\n";
    }
} // 于此调用删除器

示例

 
与另一个 shared_ptr 或 nullptr 进行比较

std::shared_ptr<T>::operator==, !=, <, <=, >, >=

比较二个 shared_ptr 对象

template < class T, class U >
bool operator==( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(1)(C++11 起)

template< class T, class U >
bool operator!=( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(2)(C++11 起)

template< class T, class U >
bool operator<( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(3)(C++11 起)

template< class T, class U >
bool operator>( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(4)(C++11 起)

template< class T, class U >
bool operator<=( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(5)(C++11 起)

template< class T, class U >
bool operator>=( const shared_ptr<T>& lhs, const shared_ptr<U>& rhs ) noexcept;

(6)(C++11 起)

比较 shared_ptr 与空指针 

template< class T >
bool operator==( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(7)(C++11 起)

template< class T >
bool operator==( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(8)(C++11 起)

template< class T >
bool operator!=( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(9)(C++11 起)

template< class T >
bool operator!=( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(10)(C++11 起)

template< class T >
bool operator<( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(11)(C++11 起)

template< class T >
bool operator<( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(12)(C++11 起)

template< class T >
bool operator>( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(13)(C++11 起)

template< class T >
bool operator>( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(14)(C++11 起)

template< class T >
bool operator<=( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(15)(C++11 起)

template< class T >
bool operator<=( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(16)(C++11 起)

template< class T >
bool operator>=( const shared_ptr<T>& lhs, std::nullptr_t rhs ) noexcept;

(17)(C++11 起)

template< class T >
bool operator>=( std::nullptr_t lhs, const shared_ptr<T>& rhs ) noexcept;

(18)(C++11 起)

比较二个 shared_ptr<T> 对象或将 shared_ptr<T> 与空指针比较。

注意 shared_ptr 的比较运算符简单地比较指针值;比较所指向的实际对象。对 shared_ptr 定义 operator< 允许将 shared_ptr 用作关联容器,如 std::map 与 std::set 中的关键。

参数

lhs-要比较的左侧 shared_ptr
rhs-要比较的右侧 shared_ptr

返回值

1) lhs.get() == rhs.get()

2) !(lhs == rhs)

3) std::less<V>()(lhs.get(), rhs.get()) ,其中 V 是 std::shared_ptr<T>::element_type* 与 std::shared_ptr<U>::element_type* 的合成指针类型

4) rhs < lhs

5) !(rhs < lhs)

6) !(lhs < rhs)

7) !lhs

8) !rhs

9) (bool)lhs

10) (bool)rhs

11) std::less<std::shared_ptr<T>::element_type*>()(lhs.get(), nullptr)

12) std::less<std::shared_ptr<T>::element_type*>()(nullptr, rhs.get())

13) nullptr < lhs

14) rhs < nullptr

15) !(nullptr < lhs)

16) !(rhs < nullptr)

17) !(lhs < nullptr)

18) !(nullptr < rhs)

注意

所有情况下,被比较者是存储的指针( get() 所返回者)而非被管理指针( uses_count 达到零时传递给删除器者)。二个指针可能在用别名使用构造函数创建的 shared_ptr 中不同。

将存储的指针的值输出到输出流

std::shared_ptr<T>::operator<<
template <class T, class U, class V>
std::basic_ostream<U, V>& operator<<
(std::basic_ostream<U, V>& os, const std::shared_ptr<T>& ptr);

插入存储于 ptr 的指针值到输出流 os 中。

等价于 os << ptr.get()

参数

os-要插入 ptr 到的 std::basic_ostream
ptr-被插入到 os 的数据

返回值

os

调用示例

#include <iostream>
#include <memory>

class Foo {};

int main()
{
    auto sp = std::make_shared<Foo>();
    std::cout << sp << std::endl;
    std::cout << sp.get() << std::endl;
}

输出


特化 std::swap 算法

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

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

参数

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

返回值

(无)

复杂度

常数

std::shared_ptr 的散列支持

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

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

对于给定的 std::shared_ptr<T> p ,此特化确保

std::hash<std::shared_ptr<T>>()(p) == std::hash<T*>()(p.get()).

(C++17 前)

std::hash<std::shared_ptr<T>>()(p) == std::hash<typename std::shared_ptr<T>::element_type*>()(p.get()).

(C++17 起)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值