shared_ptr_pointer.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===//
2//
3// ��������������������The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <memory>
11
12// shared_ptr
13
14// template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p);
15
16#include <memory>
17#include <cassert>
18
19struct B
20{
21    static int count;
22
23    B() {++count;}
24    B(const B&) {++count;}
25    ~B() {--count;}
26};
27
28int B::count = 0;
29
30struct A
31{
32    static int count;
33
34    A() {++count;}
35    A(const A&) {++count;}
36    ~A() {--count;}
37};
38
39int A::count = 0;
40
41int main()
42{
43    {
44        std::shared_ptr<A> pA(new A);
45        assert(pA.use_count() == 1);
46        {
47            B b;
48            std::shared_ptr<B> pB(pA, &b);
49            assert(A::count == 1);
50            assert(B::count == 1);
51            assert(pA.use_count() == 2);
52            assert(pB.use_count() == 2);
53            assert(pB.get() == &b);
54        }
55        assert(pA.use_count() == 1);
56        assert(A::count == 1);
57        assert(B::count == 0);
58    }
59    assert(A::count == 0);
60    assert(B::count == 0);
61}
62