Home | History | Annotate | Download | only in util.smartptr.weak.obs
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. 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 
     19 struct A
     20 {
     21     static int count;
     22 
     23     A() {++count;}
     24     A(const A&) {++count;}
     25     ~A() {--count;}
     26 };
     27 
     28 int A::count = 0;
     29 
     30 int 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