1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <memory>
10 
11 // weak_ptr
12 
13 // bool expired() const;
14 
15 #include <memory>
16 #include <cassert>
17 
18 struct A
19 {
20     static int count;
21 
22     A() {++count;}
23     A(const A&) {++count;}
24     ~A() {--count;}
25 };
26 
27 int A::count = 0;
28 
29 int main()
30 {
31     {
32         std::weak_ptr<A> wp;
33         assert(wp.use_count() == 0);
34         assert(wp.expired() == (wp.use_count() == 0));
35     }
36     {
37         std::shared_ptr<A> sp0(new A);
38         std::weak_ptr<A> wp(sp0);
39         assert(wp.use_count() == 1);
40         assert(wp.expired() == (wp.use_count() == 0));
41         sp0.reset();
42         assert(wp.use_count() == 0);
43         assert(wp.expired() == (wp.use_count() == 0));
44     }
45 }
46