Debian Codegraph @

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.weak/util.smartptr.weak.obs/expired.pass.cpp

raw
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 #include "test_macros.h"
19
20 struct A
21 {
22 static int count;
23
24 A() {++count;}
25 A(const A&) {++count;}
26 ~A() {--count;}
27 };
28
29 int A::count = 0;
30
31 int main(int, char**)
32 {
33 {
34 std::weak_ptr<A> wp;
35 assert(wp.use_count() == 0);
36 assert(wp.expired() == (wp.use_count() == 0));
37 }
38 {
39 std::shared_ptr<A> sp0(new A);
40 std::weak_ptr<A> wp(sp0);
41 assert(wp.use_count() == 1);
42 assert(wp.expired() == (wp.use_count() == 0));
43 sp0.reset();
44 assert(wp.use_count() == 0);
45 assert(wp.expired() == (wp.use_count() == 0));
46 }
47
48 return 0;
49 }