Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/creation/private.cc

raw
1 // { dg-do run { target c++11 } }
2 // { dg-require-effective-target hosted }
3
4 // Copyright (C) 2011-2025 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <memory>
22 #include <new>
23
24 // The behaviour tested here relies on the resolution of LWG issue 2070
25
26 template<typename T> struct MyAlloc;
27
28 class Private
29 {
30 Private() = default;
31 Private(const Private&) = default;
32 ~Private() = default;
33
34 friend class MyAlloc<Private>;
35
36 public:
37 int get() const { return 0; }
38 };
39
40 template<typename T>
41 struct MyAlloc : std::allocator<T>
42 {
43 template<typename U>
44 struct rebind { typedef MyAlloc<U> other; };
45
46 MyAlloc() = default;
47 MyAlloc(const MyAlloc&) = default;
48
49 template<typename U>
50 MyAlloc(const MyAlloc<U>&) { }
51
52 void construct(T* p) { ::new((void*)p) T(); }
53 void destroy(T* p) { p->~T(); }
54 };
55
56 int main()
57 {
58 MyAlloc<Private> a;
59 auto p = std::allocate_shared<Private>(a);
60 return p->get();
61 }