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 // shared_ptr
13 
14 // template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args);
15 
16 #include <memory>
17 #include <cassert>
18 
19 #include "count_new.hpp"
20 
21 struct A
22 {
23     static int count;
24 
AA25     A(int i, char c) : int_(i), char_(c) {++count;}
AA26     A(const A& a)
27         : int_(a.int_), char_(a.char_)
28         {++count;}
~AA29     ~A() {--count;}
30 
get_intA31     int get_int() const {return int_;}
get_charA32     char get_char() const {return char_;}
33 private:
34     int int_;
35     char char_;
36 };
37 
38 int A::count = 0;
39 
40 
41 struct Foo
42 {
43     Foo() = default;
44     virtual ~Foo() = default;
45 };
46 
47 
main()48 int main()
49 {
50     int nc = globalMemCounter.outstanding_new;
51     {
52     int i = 67;
53     char c = 'e';
54     std::shared_ptr<A> p = std::make_shared<A>(i, c);
55     assert(globalMemCounter.checkOutstandingNewEq(nc+1));
56     assert(A::count == 1);
57     assert(p->get_int() == 67);
58     assert(p->get_char() == 'e');
59     }
60 
61     { // https://llvm.org/bugs/show_bug.cgi?id=24137
62     std::shared_ptr<Foo> p1       = std::make_shared<Foo>();
63     assert(p1.get());
64     std::shared_ptr<const Foo> p2 = std::make_shared<const Foo>();
65     assert(p2.get());
66     }
67 
68 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
69     nc = globalMemCounter.outstanding_new;
70     {
71     char c = 'e';
72     std::shared_ptr<A> p = std::make_shared<A>(67, c);
73     assert(globalMemCounter.checkOutstandingNewEq(nc+1));
74     assert(A::count == 1);
75     assert(p->get_int() == 67);
76     assert(p->get_char() == 'e');
77     }
78 #endif
79     assert(A::count == 0);
80 }
81