【例子驱动学习】shared_ptr

本文通过实例详细解析了C++标准库中的shared_ptr智能指针,包括其构造、析构、赋值、成员函数及非成员函数等特性,并探讨了一些不易理解的概念。

用例子来学习shared_ptr
写这个是为了熟悉shared_ptr的接口,注意它与其他智能指针的区别。本测试主要来自cplusplus。

代码

/*
 * ---------------------------------------------------
 *  Copyright (c) 2017 josan All rights reserved.
 * ---------------------------------------------------
 *
 *               创建者: Josan
 *             创建时间: 2017/9/20 10:26:42
 */
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#include <memory>   //shared_ptr
#include <iostream>
#include <set>
using namespace std;

//---------------测试shared_ptr的各种成员函数  测试用例来自cplusplus.com-------

struct C
{
    int* data;
};

static void testCtor()
{
    //default constructor 
    std::shared_ptr<int> p1;
    std::shared_ptr<int> p2(nullptr);
    //ctor from null pointer 
    std::shared_ptr<int> p3(new int);
    //ctor pointer + deleter 
    std::shared_ptr<int> p4(new int, std::default_delete<int>());
    //ctor from pointer + deleter + allocator 
    std::shared_ptr<int> p5(new int, [](int* p) {delete p; }, std::allocator<int>());
    //copy constructors  
    /*
     * shared_ptr (const shared_ptr& x) noexcept;
     * template <class U> shared_ptr (const shared_ptr<U>& x) noexcept;   //注意,这是(左)引用,区分unique_ptr
     */
    std::shared_ptr<int> p6(p5);     //此时,p6.use_count()  == 2
    //move constructors  p6失去指针的所有权
    std::shared_ptr<int> p7(std::move(p6));
    //move from other types of managed pointers    也可以是auto_ptr  注意这里面的传参是右值引用
    std::shared_ptr<int> p8(std::unique_ptr<int>(new int));
    //aliasing ctor
    /*
     * template <class U> shared_ptr (const shared_ptr<U>& x, element_type* p) noexcept;
     */
    std::shared_ptr<C> obj(new C);
    std::shared_ptr<int> p9(obj, obj->data);

    //-----output------
    std::cout << "use_count:\n";
    std::cout << "p1: " << p1.use_count() << '\n';  //0
    std::cout << "p2: " << p2.use_count() << '\n';  //0
    std::cout << "p3: " << p3.use_count() << '\n';  //1
    std::cout << "p4: " << p4.use_count() << '\n';  //1
    std::cout << "p5: " << p5.use_count() << '\n';  //2
    std::cout << "p6: " << p6.use_count() << '\n';  //0
    std::cout << "p7: " << p7.use_count() << '\n';  //2
    std::cout << "p8: " << p8.use_count() << '\n';  //1
    std::cout << "p9: " << p9.use_count() << '\n';  //2
}

static void testCtorWithMyDeleter()
{
    auto deleter = [](int*p) {
        std::cout << "[deleter called]\n"; delete p;
    };
    std::shared_ptr<int> foo(new int, deleter);
    std::cout << "use_count: " << foo.use_count() << '\n';
}

static void testAssignment()
{
    std::shared_ptr<int> foo;
    std::shared_ptr<int> bar(new int(10));

    foo = bar;                          // copy
    bar = std::make_shared<int>(20);   // move

    std::unique_ptr<int> unique(new int(30));
    foo = std::move(unique);            // move from unique_ptr

    std::cout << "*foo: " << *foo << '\n';
    std::cout << "*bar: " << *bar << '\n';
}

//element_type* get() const noexcept;
static void testGet()
{
    int* p = new int(10);
    std::shared_ptr<int> a(p);

    if(a.get() == p)
        std::cout << "a and p point to the same location\n";

    // three ways of accessing the same address: 
    std::cout << *a.get() << "\n";  //10
    std::cout << *a << "\n";  //10
    std::cout << *p << "\n";  //10
}

/*
 * (1) void reset() noexcept;
 * (2) template <class U> void reset (U* p);
 * (3) template <class U, class D> void reset (U* p, D del);
 * (4) template <class U, class D, class Alloc> void reset (U* p, D del, Alloc alloc);
 */
static void testReset()
{
    auto deleter = [](int*p) {
        std::cout << "[deleter called]\n"; delete p;
    };
    std::shared_ptr<int> sp(nullptr, deleter);  // empty

    //同时将sp的deleter设置为默认的删除器
    sp.reset(new int);       // takes ownership of pointer
    *sp = 10;
    std::cout << *sp << '\n';  //10

    sp.reset(new int);       // deletes managed object, acquires new pointer
    *sp = 20;
    std::cout << *sp << '\n';  //20

    sp.reset();               // deletes managed object
}

//void swap (shared_ptr& x) noexcept;
static void testSwap()
{
    std::shared_ptr<int> foo(new int(10));
    std::shared_ptr<int> bar(new int(20));

    foo.swap(bar);

    std::cout << "*foo: " << *foo << '\n';  //20
    std::cout << "*bar: " << *bar << '\n';  //10
}

//long int use_count() const noexcept;
/*
 * Returns the number of shared_ptr objects that share ownership over the same pointer as this object (including it).
 * If this is an empty shared_ptr, the function returns zero.
 */
static void testUse_count()
{
    std::shared_ptr<int> p1;
    std::shared_ptr<int> p2(new int);
    std::cout << "p1: " << p1.use_count() << '\n';  //0
    std::cout << "p2: " << p2.use_count() << '\n';  //1
}

//bool unique() const noexcept;
static void testUnique()
{
    std::shared_ptr<int> foo;
    std::shared_ptr<int> bar(new int);

    std::cout << "foo unique?\n" << std::boolalpha;

    std::cout << "1: " << foo.unique() << '\n';  // false (empty)

    foo = bar;
    std::cout << "2: " << foo.unique() << '\n';  // false (shared with bar)

    bar = nullptr;  //释放bar的拥有权
    std::cout << "3: " << foo.unique() << '\n';  // true
}

/* template <class U> bool owner_before (const shared_ptr<U>& x) const;
 * template <class U> bool owner_before(const weak_ptr<U>& x) const;
 * Returns whether the object is considered to go before x following a strict weak owner-based order.
 */
//NOTE:对于这个函数不理解!!!
static void testOwner_before()
{
    int * p = new int(10);

    std::shared_ptr<int> a(new int(20));
    std::shared_ptr<int> b(a, p);  // alias constructor

    std::cout << "comparing a and b...\n" << std::boolalpha;
    //std::cout << "value-based: " << (!(a < b) && !(b < a)) << '\n';
    cout << a.owner_before(b) << endl;
    cout << b.owner_before(a) << endl;
    std::cout << "owner-based: " << (!a.owner_before(b) && !b.owner_before(a)) << '\n';

    std::shared_ptr<int> c(new int(20));
    std::shared_ptr<int> d(c);  // copy constructor
    cout << c.owner_before(d) << endl;
    cout << d.owner_before(c) << endl;
    delete p;
}

//测试成员函数
static void testMemberFunction()
{
    ////测试成员函数get()   注意此时源地址未失效
    //testGet();
    //测试成员函数get_deleter()
    //testGet_deleter();
    ////测试reset(),        --注意没有release(),因为可能不只是一个占用指针,因此单个shared_ptr没有权利释放
    //testReset();
    ////测试swap
    //testSwap();
    ////测试use_count()
    //testUse_count();
    ////测试unique()
    //testUnique();
    //测试owner_before()
    testOwner_before();
    //其他符号重载函数operator*, ->, bool 
}


static void testNonSwap()
{
    std::shared_ptr<int> foo(new int(10));
    std::shared_ptr<int> bar(new int(20));

    swap(foo, bar);

    std::cout << "foo: " << *foo << '\n';
    std::cout << "bar: " << *bar << '\n';
}

static void testNonRelationalOperators()
{
    std::shared_ptr<int> a, b, c, d;

    a = std::make_shared<int>(10);
    b = std::make_shared<int>(10);
    c = b;

    std::cout << "comparisons:\n" << std::boolalpha;

    std::cout << "a == b: " << (a == b) << '\n';  //false
    std::cout << "b == c: " << (b == c) << '\n';  //true
    std::cout << "c == d: " << (c == d) << '\n';  //false

    std::cout << "a != nullptr: " << (a != nullptr) << '\n';  //true
    std::cout << "b != nullptr: " << (b != nullptr) << '\n';  //true
    std::cout << "c != nullptr: " << (c != nullptr) << '\n';  //true
    std::cout << "d != nullptr: " << (d != nullptr) << '\n';  //false
}

//测试make_shared<>()
static void testNonMake_shared()
{
    std::shared_ptr<int> foo = std::make_shared<int>(10);
    // same as:
    std::shared_ptr<int> foo2(new int(10));

    auto bar = std::make_shared<int>(20);

    auto baz = std::make_shared<std::pair<int, int>>(30, 40);

    std::cout << "*foo: " << *foo << '\n';
    std::cout << "*bar: " << *bar << '\n';
    std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n';
}

//测试static_pointer_cast<>()
struct A
{
    static const char* static_type;
    const char* dynamic_type;
    A()
    {
        dynamic_type = static_type;
    }
};
struct B : A
{
    static const char* static_type;
    B()
    {
        dynamic_type = static_type;
    }
};

const char* A::static_type = "class A";
const char* B::static_type = "class B";

/*
 * template <class T, class U>
 * shared_ptr<T> static_pointer_cast (const shared_ptr<U>& sp) noexcept;
 */
static void testNonStatic_pointer_cast()
{
    std::shared_ptr<A> foo;
    std::shared_ptr<B> bar;

    foo = std::make_shared<A>();

    // statically from A* to B*.
    // cast of potentially incomplete object, but ok as a static cast:
    bar = std::static_pointer_cast<B>(foo);

    std::cout << "foo's static  type: " << foo->static_type << '\n';  //class A
    std::cout << "foo's dynamic type: " << foo->dynamic_type << '\n';  //class A
    std::cout << "bar's static  type: " << bar->static_type << '\n';  //class B
    std::cout << "bar's dynamic type: " << bar->dynamic_type << '\n';  //class A
}

/*
 * template <class T, class U>
 * shared_ptr<T> dynamic_pointer_cast (const shared_ptr<U>& sp) noexcept;
 */
static void testNonDynamic_pointer_cast()
{
    std::shared_ptr<A> foo;
    std::shared_ptr<B> bar;

    bar = std::make_shared<B>();
    //dynamically from B* to A*.
    foo = std::dynamic_pointer_cast<A>(bar);

    std::cout << "foo's static  type: " << foo->static_type << '\n';  //class A
    std::cout << "foo's dynamic type: " << foo->dynamic_type << '\n';  //class B
    std::cout << "bar's static  type: " << bar->static_type << '\n';  //class B
    std::cout << "bar's dynamic type: " << bar->dynamic_type << '\n';  //class B
}

/*
 * template <class T, class U>
 * shared_ptr<T> const_pointer_cast (const shared_ptr<U>& sp) noexcept;
 */
static void testNonConst_pointer_cast()
{
    std::shared_ptr<int> foo;
    std::shared_ptr<const int> bar;

    foo = std::make_shared<int>(10);

    //const casted from int* to const int*.
    bar = std::const_pointer_cast<const int>(foo);


    std::shared_ptr<int> foo1;
    foo1 = std::const_pointer_cast<int>(bar);

    std::cout << "*bar: " << *bar << '\n';  //10
    *foo = 20;
    std::cout << "*bar: " << *bar << '\n';  //20
    *foo1 = 30;
    std::cout << "*foo: " << *foo << '\n';  //30
    std::cout << "*bar: " << *bar << '\n';  //30
    std::cout << "*foo1: " << *foo1 << '\n';  //30
    std::cout << "use_count: " << foo.use_count() << ", " << bar.use_count() << ", " << foo1.use_count() << endl;  //3 3 3
}

struct D
{    // a verbose array deleter:
    void operator()(int* p)
    {
        std::cout << "[deleter called]\n";
        delete[] p;
    }
};

/*
 * template <class D, class T>
 * D* get_deleter (const shared_ptr<T>& sp) noexcept;
 */
static void testNonGet_deleter()
{
    std::shared_ptr<int> foo(new int[10], D());

    int * bar = new int[20];

    // use foo's deleter to delete bar (which is unmanaged):
    (*std::get_deleter<D, int>(foo))(bar);  //[deleter called]
}

/*
 * template <class Ptr> struct owner_less;
 * template <class T> struct owner_less<shared_ptr<T>>;
 * template <class T> struct owner_less<weak_ptr<T>>;
 */
static void testNonOwner_less()
{
    int * p = new int(10);

    std::shared_ptr<int> a(new int(20));
    std::shared_ptr<int> b(a, p);  // alias constructor: co-owns a, points to p

                                   // standard set container: cannot contain duplicates.
    std::set < std::shared_ptr<int> > value_based;  // uses std::less
    std::set < std::shared_ptr<int>, std::owner_less<std::shared_ptr<int>> > owner_based;

    value_based.insert(a);
    value_based.insert(b);  // ok, different value

    owner_based.insert(a);
    owner_based.insert(b);  // overwrites (same owned pointer)

    std::cout << "value_based.size() is " << value_based.size() << '\n';
    std::cout << "owner_based.size() is " << owner_based.size() << '\n';
    delete p;
}

struct C1 : std::enable_shared_from_this<C1>
{
};

static void testEnable_shared_from_this()
{
    std::shared_ptr<C1> foo, bar;

    foo = std::make_shared<C1>();

    bar = foo->shared_from_this();

    if(!foo.owner_before(bar) && !bar.owner_before(foo))
        std::cout << "foo and bar share ownership";
}

//测试非成员函数
static void testNonMemberFunction()
{
    /*testNonSwap();
    testNonRelationalOperators();*/
    ////测试make_shared()
    //testNonMake_shared();
    ////测试static_pointer_cast()
    //testNonStatic_pointer_cast();
    ////测试dynamic_pointer_cast()
    //testNonDynamic_pointer_cast();
    ////测试const_pointer_cast()
    //testNonConst_pointer_cast();
    ////测试get_deleter()
    //testNonGet_deleter();
    ////测试类模板owner_less
    //testNonOwner_less();
    //测试类模板enable_shared_from_this
    testEnable_shared_from_this();
}

//从cplusplus摘取的测试用例。
static void testFromCPlusPlus()
{
    ////测试构造函数
    //testCtor();
    ////测试带自定义删除器
    //testCtorWithMyDeleter();
    ////测试赋值函数
    //testAssignment();
    ////测试成员函数
    //testMemberFunction();
    //测试非成员函数
    testNonMemberFunction();
}

//总测试
static void test()
{
    //测试shared_ptr的各种成员函数  测试用例来自cplusplus.com
    testFromCPlusPlus();
    //测试多态,包括指针的upcast,和downcast的转换。
    //wait to test
}

int main()
{
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    test();
    return 0;
}

总结

  • shared_ptr没有成员函数release(),可以直接进行拷贝和赋值。

  • 盲点

成员函数:owner_before()

非成员函数:除了make_shared()

参考

  1. cplusplus
<think>嗯,用户想问C++中的std::function类型定义及其参数含义,具体例子是std::function<void(COM_SUB_PROCESS_MODE, const std::shared_ptr<void> &)>。我需要先理解std::function的基本概念,然后拆解这个具体类型的各个部分。 首先,std::function是C++11引入的一个通用函数封装器,可以存储、复制和调用任何可调用的目标,比如函数、lambda表达式、绑定表达式或其他函数对象。用户给出的例子是一个返回void,接受两个参数的函数对象:第一个参数是COM_SUB_PROCESS_MODE类型,第二个是const std::shared_ptr<void>&。 接下来,我需要解释这个类型定义的具体结构。模板参数中的void表示函数没有返回值,括号内的参数分别是两个参数的类型。第一个参数可能是一个枚举或结构体,具体取决于COM_SUB_PROCESS_MODE的定义,用户可能需要查阅相关文档。第二个参数是一个共享指针的常量引用,指向void类型,这种设计通常用于传递任意类型的数据,使用时需要强制类型转换。 然后,关于用法部分,应该举例说明如何初始化这个std::function对象,比如用lambda、函数指针或绑定表达式。同时,需要提到调用时的注意事项,比如参数传递和类型安全。可能还需要提到使用shared_ptr<void>的好处,比如资源管理和多态性。 用户可能还想知道为什么使用std::function而不是普通函数指针,这里可以提到std::function的灵活性,能够容纳更多可调用对象。此外,可能需要提醒用户关于类型擦除的问题,以及性能上的考虑,比如std::function可能会有一定的开销。 还要注意用户提供的引用内容中有spinlock和JNIEnv相关的引用,但似乎和当前问题关联不大,可能不需要特别处理,除非有相关的内容可以结合,但看起来可能用户误贴了引用内容。因此,在回答中可以忽略这些引用,专注于std::function的解释。 最后,生成相关问题需要考虑用户可能进一步的问题,比如与其他函数包装器的比较、性能优化、参数传递方式或者类型转换的具体方法等。需要确保问题覆盖用户可能感兴趣的扩展方向。</think>### C++中`std::function`类型定义及参数含义解析 #### 1. **`std::function` 基本定义** `std::function` 是C++标准库提供的通用函数封装器,用于存储、复制和调用任何可调用的目标(如函数、Lambda表达式、绑定表达式、函数对象等)[^1]。其模板参数格式为: $$std::function<返回类型(参数类型1, 参数类型2, ...)>$$ #### 2. **具体类型拆解** 用户提供的类型为: ```cpp std::function<void(COM_SUB_PROCESS_MODE, const std::shared_ptr<void>&)> ``` - **`void`**:表示函数无返回值。 - **`COM_SUB_PROCESS_MODE`**:第一个参数类型,可能是一个枚举或结构体,需结合具体业务定义(例如表示子进程的模式)。 - **`const std::shared_ptr<void>&`**:第二个参数类型,是一个常量引用的共享指针,指向`void`类型数据,常用于传递任意类型的对象(需运行时类型转换)。 #### 3. **典型用法示例** ```cpp // 定义函数类型 using Callback = std::function<void(COM_SUB_PROCESS_MODE, const std::shared_ptr<void>&)>; // 示例1:Lambda表达式赋值 Callback func1 = [](COM_SUB_PROCESS_MODE mode, const std::shared_ptr<void>& data) { // 处理逻辑 }; // 示例2:绑定成员函数 class Processor { public: void handle(COM_SUB_PROCESS_MODE mode, const std::shared_ptr<void>& data) { // 处理逻辑 } }; Processor processor; Callback func2 = std::bind(&Processor::handle, &processor, std::placeholders::_1, std::placeholders::_2); // 调用函数 func1(COM_SUB_PROCESS_MODE::DEFAULT, std::make_shared<int>(42)); ``` #### 4. **参数传递注意事项** - **`std::shared_ptr<void>`**:通过共享指针传递数据可自动管理内存生命周期。使用时需通过`static_cast`或`dynamic_cast`转换为具体类型: ```cpp auto data = std::static_pointer_cast<MyType>(shared_ptr_void); ``` - **`COM_SUB_PROCESS_MODE`**:需确保调用方和接收方对此枚举或结构体的定义一致。 #### 5. **应用场景** - **回调机制**:用于事件驱动编程(如异步任务完成通知)。 - **解耦接口**:允许将函数作为参数传递,增强代码灵活性[^1]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值