Home | History | Annotate | Download | only in util.smartptr.shared.create
      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 <new>
     18 #include <cstdlib>
     19 #include <cassert>
     20 
     21 int new_count = 0;
     22 
     23 void* operator new(std::size_t s) throw(std::bad_alloc)
     24 {
     25     ++new_count;
     26     return std::malloc(s);
     27 }
     28 
     29 void  operator delete(void* p) throw()
     30 {
     31     std::free(p);
     32 }
     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 private:
     47     int int_;
     48     char char_;
     49 };
     50 
     51 int A::count = 0;
     52 
     53 int main()
     54 {
     55     int nc = new_count;
     56     {
     57     int i = 67;
     58     char c = 'e';
     59     std::shared_ptr<A> p = std::make_shared<A>(i, c);
     60     assert(new_count == nc+1);
     61     assert(A::count == 1);
     62     assert(p->get_int() == 67);
     63     assert(p->get_char() == 'e');
     64     }
     65 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     66     nc = new_count;
     67     {
     68     char c = 'e';
     69     std::shared_ptr<A> p = std::make_shared<A>(67, c);
     70     assert(new_count == nc+1);
     71     assert(A::count == 1);
     72     assert(p->get_int() == 67);
     73     assert(p->get_char() == 'e');
     74     }
     75 #endif
     76     assert(A::count == 0);
     77 }
     78