make_shared.pass.cpp (2565B)
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 "test_macros.h" 20 #include "count_new.hpp" 21 22 #if TEST_STD_VER >= 11 23 #define DELETE_FUNCTION = delete 24 #else 25 #define DELETE_FUNCTION 26 #endif 27 28 struct A 29 { 30 static int count; 31 32 A(int i, char c) : int_(i), char_(c) {++count;} 33 A(const A& a) 34 : int_(a.int_), char_(a.char_) 35 {++count;} 36 ~A() {--count;} 37 38 int get_int() const {return int_;} 39 char get_char() const {return char_;} 40 41 A* operator& () DELETE_FUNCTION; 42 43 private: 44 int int_; 45 char char_; 46 }; 47 48 int A::count = 0; 49 50 51 struct Foo 52 { 53 Foo() = default; 54 virtual ~Foo() = default; 55 }; 56 57 #ifdef _LIBCPP_VERSION 58 struct Result {}; 59 static Result theFunction() { return Result(); } 60 static int resultDeletorCount; 61 static void resultDeletor(Result (*pf)()) { 62 assert(pf == theFunction); 63 ++resultDeletorCount; 64 } 65 66 void test_pointer_to_function() { 67 { // https://bugs.llvm.org/show_bug.cgi?id=27566 68 std::shared_ptr<Result()> x(&theFunction, &resultDeletor); 69 std::shared_ptr<Result()> y(theFunction, resultDeletor); 70 } 71 assert(resultDeletorCount == 2); 72 } 73 #else // _LIBCPP_VERSION 74 void test_pointer_to_function() {} 75 #endif // _LIBCPP_VERSION 76 77 int main() 78 { 79 int nc = globalMemCounter.outstanding_new; 80 { 81 int i = 67; 82 char c = 'e'; 83 std::shared_ptr<A> p = std::make_shared<A>(i, c); 84 assert(globalMemCounter.checkOutstandingNewEq(nc+1)); 85 assert(A::count == 1); 86 assert(p->get_int() == 67); 87 assert(p->get_char() == 'e'); 88 } 89 90 { // https://bugs.llvm.org/show_bug.cgi?id=24137 91 std::shared_ptr<Foo> p1 = std::make_shared<Foo>(); 92 assert(p1.get()); 93 std::shared_ptr<const Foo> p2 = std::make_shared<const Foo>(); 94 assert(p2.get()); 95 } 96 97 test_pointer_to_function(); 98 99 #if TEST_STD_VER >= 11 100 nc = globalMemCounter.outstanding_new; 101 { 102 char c = 'e'; 103 std::shared_ptr<A> p = std::make_shared<A>(67, c); 104 assert(globalMemCounter.checkOutstandingNewEq(nc+1)); 105 assert(A::count == 1); 106 assert(p->get_int() == 67); 107 assert(p->get_char() == 'e'); 108 } 109 #endif 110 assert(A::count == 0); 111 }