]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/ext/new_allocator/89510.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / new_allocator / 89510.cc
CommitLineData
99dee823 1// Copyright (C) 2020-2021 Free Software Foundation, Inc.
b1983f45
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-do compile { target c++11 } }
19
20#include <ext/new_allocator.h>
21#include <memory>
22#include <type_traits>
23
24using __gnu_cxx::new_allocator;
25using AT = std::allocator_traits<new_allocator<int>>;
26
27template<typename...> using void_t = void;
28
29template<typename T, typename U, typename = void>
30struct has_construct
31: std::false_type
32{ };
33
34template<typename T, typename U>
35struct has_construct<T, U,
36 void_t<decltype(std::declval<T&>().construct(std::declval<U*>()))>>
37: std::true_type
38{ };
39
40template<typename T, typename U, typename = void>
41struct has_destroy
42: std::false_type
43{ };
44
45template<typename T, typename U>
46struct has_destroy<T, U,
47 void_t<decltype(std::declval<T&>().destroy(std::declval<U*>()))>>
48: std::true_type
49{ };
50
51template<typename T, typename U, typename = void>
52struct has_traits_construct
53: std::false_type
54{ };
55
56template<typename T, typename U>
57struct has_traits_construct<T, U,
58 void_t<decltype(AT::construct(std::declval<T&>(), std::declval<U*>()))>>
59: std::true_type
60{ };
61
62template<typename T, typename U, typename = void>
63struct has_traits_destroy
64: std::false_type
65{ };
66
67template<typename T, typename U>
68struct has_traits_destroy<T, U,
69 void_t<decltype(AT::destroy(std::declval<T&>(), std::declval<U*>()))>>
70: std::true_type
71{ };
72
73struct NoDefault { NoDefault(int); };
74struct NoDest { private: ~NoDest(); };
75
76// Whether true or false, these should not give errors:
77constexpr bool c = has_construct<new_allocator<NoDefault>, NoDefault>::value;
78constexpr bool c2 = has_traits_construct<new_allocator<int>, NoDefault>::value;
79constexpr bool d = has_destroy<new_allocator<NoDest>, NoDest>::value;
80constexpr bool d2 = has_traits_destroy<new_allocator<int>, NoDest>::value;
81
82new_allocator<int> a;
83
84long* lp;
85#if __cplusplus <= 201703L
86static_assert( noexcept(a.construct(lp)), "" );
87static_assert( noexcept(a.construct(lp, 1L)), "" );
88static_assert( noexcept(a.construct(lp, 2)), "" );
89static_assert( noexcept(a.construct(lp, 2U)), "" );
90static_assert( noexcept(a.destroy(lp)), "" );
91#endif
92static_assert( noexcept(AT::construct(a, lp)), "" );
93static_assert( noexcept(AT::construct(a, lp, 1L)), "" );
94static_assert( noexcept(AT::construct(a, lp, 2)), "" );
95static_assert( noexcept(AT::construct(a, lp, 2U)), "" );
96static_assert( noexcept(AT::destroy(a, lp)), "" );
97
98struct X
99{
100 X() noexcept;
101 X(int) noexcept;
102 ~X() noexcept;
103};
104
105X* xp;
106#if __cplusplus <= 201703L
107static_assert( noexcept(a.construct(xp)), "" );
108static_assert( noexcept(a.construct(xp, 1)), "" );
109static_assert( noexcept(a.destroy(xp)), "" );
110#endif
111static_assert( noexcept(AT::construct(a, xp)), "" );
112static_assert( noexcept(AT::construct(a, xp, 1)), "" );
113static_assert( noexcept(AT::destroy(a, xp)), "" );
114
115struct Y
116{
117 Y() noexcept;
118 Y(int) noexcept(false);
119 ~Y() noexcept;
120};
121
122Y* yp;
123#if __cplusplus <= 201703L
124static_assert( noexcept(a.construct(yp)), "" );
125static_assert( ! noexcept(a.construct(yp, 1)), "" );
126static_assert( noexcept(a.destroy(yp)), "" );
127#endif
128static_assert( noexcept(AT::construct(a, yp)), "" );
129static_assert( ! noexcept(AT::construct(a, yp, 1)), "" );
130static_assert( noexcept(AT::destroy(a, yp)), "" );
131
132struct Z
133{
134 Z() noexcept;
135 Z(int) noexcept;
136 ~Z() noexcept(false);
137};
138
139Z* zp;
140// These construct calls should be noexcept, but they are false because
141// they use is_nothrow_constructible which depends on is_nothrow_destructible.
142#if __cplusplus <= 201703L
143static_assert( ! noexcept(a.construct(zp)), "wrong" );
144static_assert( ! noexcept(a.construct(zp, 1)), "wrong" );
145static_assert( ! noexcept(a.destroy(zp)), "" );
146#endif
147static_assert( ! noexcept(AT::construct(a, zp)), "" );
148static_assert( ! noexcept(AT::construct(a, zp, 1)), "" );
149static_assert( ! noexcept(AT::destroy(a, zp)), "" );