expired.pass.cpp revision c52f43e72dfcea03037729649da84c23b3beb04a
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// weak_ptr
13
14// bool expired() const;
15
16#include <memory>
17#include <cassert>
18
19struct A
20{
21    static int count;
22
23    A() {++count;}
24    A(const A&) {++count;}
25    ~A() {--count;}
26};
27
28int A::count = 0;
29
30int main()
31{
32    {
33        std::weak_ptr<A> wp;
34        assert(wp.use_count() == 0);
35        assert(wp.expired() == (wp.use_count() == 0));
36    }
37    {
38        std::shared_ptr<A> sp0(new A);
39        std::weak_ptr<A> wp(sp0);
40        assert(wp.use_count() == 1);
41        assert(wp.expired() == (wp.use_count() == 0));
42        sp0.reset();
43        assert(wp.use_count() == 0);
44        assert(wp.expired() == (wp.use_count() == 0));
45    }
46}
47