Home | History | Annotate | Download | only in unique.ptr.single.ctor
      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 // unique_ptr
     13 
     14 //=============================================================================
     15 // TESTING std::unique_ptr::unique_ptr()
     16 //
     17 // Concerns:
     18 //   1 The default constructor works for any default constructible deleter types.
     19 //   2 The stored type 'T' is allowed to be incomplete.
     20 //
     21 // Plan
     22 //  1 Default construct unique_ptr's with various deleter types (C-1)
     23 //  2 Default construct a unique_ptr with a incomplete element_type and
     24 //    various deleter types (C-1,2)
     25 
     26 #include <memory>
     27 #include <cassert>
     28 
     29 #include "../../deleter.h"
     30 
     31 struct IncompleteT;
     32 
     33 void checkNumIncompleteTypeAlive(int i);
     34 
     35 template <class Del = std::default_delete<IncompleteT> >
     36 struct StoresIncomplete {
     37   std::unique_ptr<IncompleteT, Del> m_ptr;
     38   StoresIncomplete() {}
     39   ~StoresIncomplete();
     40 
     41   IncompleteT* get() const { return m_ptr.get(); }
     42   Del& get_deleter() { return m_ptr.get_deleter(); }
     43 };
     44 
     45 int main()
     46 {
     47     {
     48       std::unique_ptr<int> p;
     49       assert(p.get() == 0);
     50     }
     51     {
     52       std::unique_ptr<int, NCDeleter<int> > p;
     53       assert(p.get() == 0);
     54       assert(p.get_deleter().state() == 0);
     55       p.get_deleter().set_state(5);
     56       assert(p.get_deleter().state() == 5);
     57     }
     58     {
     59         StoresIncomplete<> s;
     60         assert(s.get() == 0);
     61         checkNumIncompleteTypeAlive(0);
     62     }
     63     checkNumIncompleteTypeAlive(0);
     64     {
     65         StoresIncomplete< Deleter<IncompleteT> > s;
     66         assert(s.get() == 0);
     67         assert(s.get_deleter().state() == 0);
     68         checkNumIncompleteTypeAlive(0);
     69     }
     70     checkNumIncompleteTypeAlive(0);
     71 }
     72 
     73 struct IncompleteT {
     74     static int count;
     75     IncompleteT() { ++count; }
     76     ~IncompleteT() {--count; }
     77 };
     78 
     79 int IncompleteT::count = 0;
     80 
     81 void checkNumIncompleteTypeAlive(int i) {
     82     assert(IncompleteT::count == i);
     83 }
     84 
     85 template <class Del>
     86 StoresIncomplete<Del>::~StoresIncomplete() { }