allocate_shared.pass.cpp (1986B)
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 // UNSUPPORTED: c++98, c++03 11 12 // <memory> 13 14 // shared_ptr 15 16 // template<class T, class A, class... Args> 17 // shared_ptr<T> allocate_shared(const A& a, Args&&... args); 18 19 #include <memory> 20 #include <new> 21 #include <cstdlib> 22 #include <cassert> 23 #include "test_allocator.h" 24 #include "min_allocator.h" 25 26 #if TEST_STD_VER >= 11 27 #define DELETE_FUNCTION = delete 28 #else 29 #define DELETE_FUNCTION 30 #endif 31 32 int new_count = 0; 33 34 struct A 35 { 36 static int count; 37 38 A(int i, char c) : int_(i), char_(c) {++count;} 39 A(const A& a) 40 : int_(a.int_), char_(a.char_) 41 {++count;} 42 ~A() {--count;} 43 44 int get_int() const {return int_;} 45 char get_char() const {return char_;} 46 47 A* operator& () DELETE_FUNCTION; 48 private: 49 int int_; 50 char char_; 51 }; 52 53 int A::count = 0; 54 55 int main() 56 { 57 { 58 int i = 67; 59 char c = 'e'; 60 std::shared_ptr<A> p = std::allocate_shared<A>(test_allocator<A>(54), i, c); 61 assert(test_allocator<A>::alloc_count == 1); 62 assert(A::count == 1); 63 assert(p->get_int() == 67); 64 assert(p->get_char() == 'e'); 65 } 66 assert(A::count == 0); 67 assert(test_allocator<A>::alloc_count == 0); 68 { 69 int i = 67; 70 char c = 'e'; 71 std::shared_ptr<A> p = std::allocate_shared<A>(min_allocator<void>(), i, c); 72 assert(A::count == 1); 73 assert(p->get_int() == 67); 74 assert(p->get_char() == 'e'); 75 } 76 assert(A::count == 0); 77 { 78 int i = 68; 79 char c = 'f'; 80 std::shared_ptr<A> p = std::allocate_shared<A>(bare_allocator<void>(), i, c); 81 assert(A::count == 1); 82 assert(p->get_int() == 68); 83 assert(p->get_char() == 'f'); 84 } 85 assert(A::count == 0); 86 }