aboutsummaryrefslogtreecommitdiff
path: root/test/std/utilities/memory/unique.ptr/unique.ptr.single/unique.ptr.single.ctor/default.pass.cpp
blob: 2694538145b90c604875f31e37d91848b27c01e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <memory>

// unique_ptr

//=============================================================================
// TESTING std::unique_ptr::unique_ptr()
//
// Concerns:
//   1 The default constructor works for any default constructible deleter types.
//   2 The stored type 'T' is allowed to be incomplete.
//
// Plan
//  1 Default construct unique_ptr's with various deleter types (C-1)
//  2 Default construct a unique_ptr with a incomplete element_type and
//    various deleter types (C-1,2)

#include <memory>
#include <cassert>

#include "../../deleter.h"

struct IncompleteT;

void checkNumIncompleteTypeAlive(int i);

template <class Del = std::default_delete<IncompleteT> >
struct StoresIncomplete {
  std::unique_ptr<IncompleteT, Del> m_ptr;
  StoresIncomplete() {}
  ~StoresIncomplete();

  IncompleteT* get() const { return m_ptr.get(); }
  Del& get_deleter() { return m_ptr.get_deleter(); }
};

int main()
{
    {
      std::unique_ptr<int> p;
      assert(p.get() == 0);
    }
    {
      std::unique_ptr<int, NCDeleter<int> > p;
      assert(p.get() == 0);
      assert(p.get_deleter().state() == 0);
      p.get_deleter().set_state(5);
      assert(p.get_deleter().state() == 5);
    }
    {
        StoresIncomplete<> s;
        assert(s.get() == 0);
        checkNumIncompleteTypeAlive(0);
    }
    checkNumIncompleteTypeAlive(0);
    {
        StoresIncomplete< Deleter<IncompleteT> > s;
        assert(s.get() == 0);
        assert(s.get_deleter().state() == 0);
        checkNumIncompleteTypeAlive(0);
    }
    checkNumIncompleteTypeAlive(0);
}

struct IncompleteT {
    static int count;
    IncompleteT() { ++count; }
    ~IncompleteT() {--count; }
};

int IncompleteT::count = 0;

void checkNumIncompleteTypeAlive(int i) {
    assert(IncompleteT::count == i);
}

template <class Del>
StoresIncomplete<Del>::~StoresIncomplete() { }