1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // template<class T, class... Args>
14 // shared_ptr<T> make_shared(Args&&... args); // T is not an array
19 #include "count_new.h"
20 #include "operator_hijacker.h"
21 #include "test_macros.h"
27 A(int i
, char c
) : int_(i
), char_(c
) {++count
;}
29 : int_(a
.int_
), char_(a
.char_
)
33 int get_int() const {return int_
;}
34 char get_char() const {return char_
;}
36 A
* operator& () = delete;
49 virtual ~Foo() = default;
52 #ifdef _LIBCPP_VERSION
54 static Result
theFunction() { return Result(); }
55 static int resultDeletorCount
;
56 static void resultDeletor(Result (*pf
)()) {
57 assert(pf
== theFunction
);
61 void test_pointer_to_function() {
62 { // https://llvm.org/PR27566
63 std::shared_ptr
<Result()> x(&theFunction
, &resultDeletor
);
64 std::shared_ptr
<Result()> y(theFunction
, resultDeletor
);
66 assert(resultDeletorCount
== 2);
68 #else // _LIBCPP_VERSION
69 void test_pointer_to_function() {}
70 #endif // _LIBCPP_VERSION
73 void test(const T
&t0
)
77 std::shared_ptr
<T
> p0
= std::make_shared
<T
>(t0
);
78 std::shared_ptr
<T
> p1
= std::make_shared
<T
>(t1
);
85 std::shared_ptr
<const T
> p0
= std::make_shared
<const T
>(t0
);
86 std::shared_ptr
<const T
> p1
= std::make_shared
<const T
>(t1
);
94 int nc
= globalMemCounter
.outstanding_new
;
98 std::shared_ptr
<A
> p
= std::make_shared
<A
>(i
, c
);
99 assert(globalMemCounter
.checkOutstandingNewLessThanOrEqual(nc
+1));
100 assert(A::count
== 1);
101 assert(p
->get_int() == 67);
102 assert(p
->get_char() == 'e');
105 { // https://llvm.org/PR24137
106 std::shared_ptr
<Foo
> p1
= std::make_shared
<Foo
>();
108 std::shared_ptr
<const Foo
> p2
= std::make_shared
<const Foo
>();
112 test_pointer_to_function();
114 #if TEST_STD_VER >= 11
115 nc
= globalMemCounter
.outstanding_new
;
118 std::shared_ptr
<A
> p
= std::make_shared
<A
>(67, c
);
119 assert(globalMemCounter
.checkOutstandingNewLessThanOrEqual(nc
+1));
120 assert(A::count
== 1);
121 assert(p
->get_int() == 67);
122 assert(p
->get_char() == 'e');
125 assert(A::count
== 0);
127 // Make sure std::make_shared handles badly-behaved types properly
129 std::shared_ptr
<operator_hijacker
> p1
= std::make_shared
<operator_hijacker
>();
130 std::shared_ptr
<operator_hijacker
> p2
= std::make_shared
<operator_hijacker
>(operator_hijacker());
131 assert(p1
!= nullptr);
132 assert(p2
!= nullptr);