File: construct.pass.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (180 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download | duplicates (3)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: clang-8
// UNSUPPORTED: gcc-8, gcc-9

// <memory>

// template <class Alloc>
// struct allocator_traits
// {
//     template <class Ptr, class... Args>
//     static constexpr void construct(allocator_type& a, Ptr p, Args&&... args);
//     ...
// };

#include <memory>
#include <new>
#include <type_traits>
#include <cassert>

#include "test_macros.h"
#include "incomplete_type_helper.h"

template <class T>
struct A
{
    typedef T value_type;

};

template <class T>
struct B
{
    typedef T value_type;

    TEST_CONSTEXPR_CXX20 B(int& count) : count_(count) {}

#if TEST_STD_VER >= 11
    template <class U, class ...Args>
    TEST_CONSTEXPR_CXX20 void construct(U* p, Args&& ...args)
    {
        ++count_;
#if TEST_STD_VER > 17
        std::construct_at(p, std::forward<Args>(args)...);
#else
        ::new ((void*)p) U(std::forward<Args>(args)...);
#endif
    }
#endif

    int& count_;
};

struct A0
{
    TEST_CONSTEXPR_CXX20 A0(int* count) {++*count;}
};

struct A1
{
    TEST_CONSTEXPR_CXX20 A1(int* count, char c)
    {
        assert(c == 'c');
        ++*count;
    }
};

struct A2
{
    TEST_CONSTEXPR_CXX20 A2(int* count, char c, int i)
    {
        assert(c == 'd');
        assert(i == 5);
        ++*count;
    }
};

TEST_CONSTEXPR_CXX20 bool test()
{
    {
        int A0_count = 0;
        A<A0> a;
        std::allocator<A0> alloc;
        A0* a0 = alloc.allocate(1);
        assert(A0_count == 0);
        std::allocator_traits<A<A0> >::construct(a, a0, &A0_count);
        assert(A0_count == 1);
        alloc.deallocate(a0, 1);
    }
    {
        int A1_count = 0;
        A<A1> a;
        std::allocator<A1> alloc;
        A1* a1 = alloc.allocate(1);
        assert(A1_count == 0);
        std::allocator_traits<A<A1> >::construct(a, a1, &A1_count, 'c');
        assert(A1_count == 1);
        alloc.deallocate(a1, 1);
    }
    {
        int A2_count = 0;
        A<A2> a;
        std::allocator<A2> alloc;
        A2* a2 = alloc.allocate(1);
        assert(A2_count == 0);
        std::allocator_traits<A<A2> >::construct(a, a2, &A2_count, 'd', 5);
        assert(A2_count == 1);
        alloc.deallocate(a2, 1);
    }
    {
      typedef IncompleteHolder* VT;
      typedef A<VT> Alloc;
      Alloc a;
      std::allocator<VT> alloc;
      VT* vt = alloc.allocate(1);
      std::allocator_traits<Alloc>::construct(a, vt, nullptr);
      alloc.deallocate(vt, 1);
    }

#if TEST_STD_VER >= 11
    {
        int A0_count = 0;
        int b_construct = 0;
        B<A0> b(b_construct);
        std::allocator<A0> alloc;
        A0* a0 = alloc.allocate(1);
        assert(A0_count == 0);
        assert(b_construct == 0);
        std::allocator_traits<B<A0> >::construct(b, a0, &A0_count);
        assert(A0_count == 1);
        assert(b_construct == 1);
        alloc.deallocate(a0, 1);
    }
    {
        int A1_count = 0;
        int b_construct = 0;
        B<A1> b(b_construct);
        std::allocator<A1> alloc;
        A1* a1 = alloc.allocate(1);
        assert(A1_count == 0);
        assert(b_construct == 0);
        std::allocator_traits<B<A1> >::construct(b, a1, &A1_count, 'c');
        assert(A1_count == 1);
        assert(b_construct == 1);
        alloc.deallocate(a1, 1);
    }
    {
        int A2_count = 0;
        int b_construct = 0;
        B<A2> b(b_construct);
        std::allocator<A2> alloc;
        A2* a2 = alloc.allocate(1);
        assert(A2_count == 0);
        assert(b_construct == 0);
        std::allocator_traits<B<A2> >::construct(b, a2, &A2_count, 'd', 5);
        assert(A2_count == 1);
        assert(b_construct == 1);
        alloc.deallocate(a2, 1);
    }
#endif

    return true;
}

int main(int, char**)
{
    test();

#if TEST_STD_VER > 17
    static_assert(test());
#endif

    return 0;
}