Esempio n. 1
0
int main()
{
    static_assert(( std::is_convertible<std::weak_ptr<A>, std::weak_ptr<B> >::value), "");
    static_assert((!std::is_convertible<std::weak_ptr<B>, std::weak_ptr<A> >::value), "");
    static_assert((!std::is_convertible<std::weak_ptr<A>, std::weak_ptr<C> >::value), "");
    {
        const std::weak_ptr<A> pA(std::shared_ptr<A>(new A));
        assert(pA.use_count() == 0);
        assert(B::count == 0);
        assert(A::count == 0);
        {
            std::weak_ptr<B> pB(pA);
            assert(B::count == 0);
            assert(A::count == 0);
            assert(pB.use_count() == 0);
            assert(pA.use_count() == 0);
        }
        assert(pA.use_count() == 0);
        assert(B::count == 0);
        assert(A::count == 0);
    }
    assert(B::count == 0);
    assert(A::count == 0);
    {
        std::weak_ptr<A> pA;
        assert(pA.use_count() == 0);
        assert(B::count == 0);
        assert(A::count == 0);
        {
            std::weak_ptr<B> pB(pA);
            assert(B::count == 0);
            assert(A::count == 0);
            assert(pB.use_count() == 0);
            assert(pA.use_count() == 0);
        }
        assert(pA.use_count() == 0);
        assert(B::count == 0);
        assert(A::count == 0);
    }
    assert(B::count == 0);
    assert(A::count == 0);
}
Esempio n. 2
0
/* Notes *
    * std::weak_ptr *
    expired - checks whether the referenced object was already deleted
    lock    - creates a shared_ptr that manages the referenced object 

    * std::unique_ptr * ,using this instead of auto_ptr considering:
    1 - operators supported(==, !=, < ...),so they can be used/stored in STL
        which uses/relies on a lot of these operations
    2 - array supported, can point to an array
*/
void f()
{
    if ( !wp.expired()) {
        auto spt = wp.lock();
        std::cout << *spt << "\n";
        std::cout << wp.use_count() << "\n";
    }
    else {
        std::cout << "wp is expired\n";
    }
}