What is the difference between an empty and a null std::shared_ptr in C++?

前端 未结 2 1312
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:37

The cplusplus.com shared_ptr page calls out a distinction between an empty std::shared_ptr and a null shared_ptr. The cppreferenc

2条回答
  •  再見小時候
    2020-12-02 07:19

    Is there a difference between an empty and a null shared_ptr?

    Empty shared_ptr doesn't have control block and its use count considered to be 0. Copy of empty shared_ptr is another empty shared_ptr. They are both separate shared_ptrs that doesn't share common control block because they don't have it. Empty shared_ptr can be constructed with default constructor or with constructor that takes nullptr.

    Non-empty null shared_ptr has control block that can be shared with other shared_ptrs. Copy of non-empty null shared_ptr is shared_ptr that shares the same control block as original shared_ptr so use count is not 0. It can be said that all copies of shared_ptr share the same nullptr. Non-empty null shared_ptr can be constructed with null pointer of object's type (not nullptr)

    Here is example:

    #include 
    #include 
    
    int main()
    {
        std::cout << "std::shared_ptr ptr1:" << std::endl;
        {
            std::shared_ptr ptr1;
            std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
            std::shared_ptr ptr2 = ptr1;
            std::cout << "\tuse count  after copying ptr: " << ptr1.use_count() << std::endl;        
            std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
        }
        std::cout << std::endl;
    
        std::cout << "std::shared_ptr ptr1(nullptr):" << std::endl;
        {
            std::shared_ptr ptr1(nullptr);
            std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
            std::shared_ptr ptr2 = ptr1;
            std::cout << "\tuse count  after copying ptr: " << ptr1.use_count() << std::endl;        
            std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
        }
        std::cout << std::endl;
    
        std::cout << "std::shared_ptr ptr1(static_cast(nullptr))" << std::endl;
        {
            std::shared_ptr ptr1(static_cast(nullptr));
            std::cout << "\tuse count before copying ptr: " << ptr1.use_count() << std::endl;
            std::shared_ptr ptr2 = ptr1;
            std::cout << "\tuse count  after copying ptr: " << ptr1.use_count() << std::endl;        
            std::cout << "\tptr1 is " << (ptr1 ? "not null" : "null") << std::endl;
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    It outputs:

    std::shared_ptr ptr1:
        use count before copying ptr: 0
        use count  after copying ptr: 0
        ptr1 is null
    
    std::shared_ptr ptr1(nullptr):
        use count before copying ptr: 0
        use count  after copying ptr: 0
        ptr1 is null
    
    std::shared_ptr ptr1(static_cast(nullptr))
        use count before copying ptr: 1
        use count  after copying ptr: 2
        ptr1 is null
    

    http://coliru.stacked-crooked.com/a/54f59730905ed2ff

提交回复
热议问题