15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <memory>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // weak_ptr
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // shared_ptr<T> lock() const;
145a83710eSEric Fiselier 
155a83710eSEric Fiselier #include <memory>
165a83710eSEric Fiselier #include <cassert>
175a83710eSEric Fiselier 
18*7fc6a556SMarshall Clow #include "test_macros.h"
19*7fc6a556SMarshall Clow 
205a83710eSEric Fiselier struct A
215a83710eSEric Fiselier {
225a83710eSEric Fiselier     static int count;
235a83710eSEric Fiselier 
AA245a83710eSEric Fiselier     A() {++count;}
AA255a83710eSEric Fiselier     A(const A&) {++count;}
~AA265a83710eSEric Fiselier     ~A() {--count;}
275a83710eSEric Fiselier };
285a83710eSEric Fiselier 
295a83710eSEric Fiselier int A::count = 0;
305a83710eSEric Fiselier 
main(int,char **)312df59c50SJF Bastien int main(int, char**)
325a83710eSEric Fiselier {
335a83710eSEric Fiselier     {
345a83710eSEric Fiselier         std::weak_ptr<A> wp;
355a83710eSEric Fiselier         std::shared_ptr<A> sp = wp.lock();
365a83710eSEric Fiselier         assert(sp.use_count() == 0);
375a83710eSEric Fiselier         assert(sp.get() == 0);
385a83710eSEric Fiselier         assert(A::count == 0);
395a83710eSEric Fiselier     }
405a83710eSEric Fiselier     {
415a83710eSEric Fiselier         std::shared_ptr<A> sp0(new A);
425a83710eSEric Fiselier         std::weak_ptr<A> wp(sp0);
435a83710eSEric Fiselier         std::shared_ptr<A> sp = wp.lock();
445a83710eSEric Fiselier         assert(sp.use_count() == 2);
455a83710eSEric Fiselier         assert(sp.get() == sp0.get());
465a83710eSEric Fiselier         assert(A::count == 1);
475a83710eSEric Fiselier     }
485a83710eSEric Fiselier     assert(A::count == 0);
495a83710eSEric Fiselier     {
505a83710eSEric Fiselier         std::shared_ptr<A> sp0(new A);
515a83710eSEric Fiselier         std::weak_ptr<A> wp(sp0);
525a83710eSEric Fiselier         sp0.reset();
535a83710eSEric Fiselier         std::shared_ptr<A> sp = wp.lock();
545a83710eSEric Fiselier         assert(sp.use_count() == 0);
555a83710eSEric Fiselier         assert(sp.get() == 0);
565a83710eSEric Fiselier         assert(A::count == 0);
575a83710eSEric Fiselier     }
585a83710eSEric Fiselier     assert(A::count == 0);
592df59c50SJF Bastien 
602df59c50SJF Bastien   return 0;
615a83710eSEric Fiselier }
62