1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 11 // We voluntarily use std::default_initializable on types that have redundant 12 // or ignored cv-qualifiers -- don't warn about it. 13 // ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-ignored-qualifiers 14 15 // template<class T> 16 // concept default_initializable = constructible_from<T> && 17 // requires { T{}; } && 18 // is-default-initializable<T>; 19 20 #include <array> 21 #include <concepts> 22 #include <deque> 23 #include <forward_list> 24 #include <list> 25 #include <map> 26 #include <memory> 27 #include <queue> 28 #include <set> 29 #include <span> 30 #include <stack> 31 #include <string> 32 #include <string_view> 33 #include <unordered_map> 34 #include <unordered_set> 35 #include <vector> 36 37 #include "test_macros.h" 38 39 struct Empty {}; 40 41 struct CtorDefaulted { 42 CtorDefaulted() = default; 43 }; 44 struct CtorDeleted { 45 CtorDeleted() = delete; 46 }; 47 struct DtorDefaulted { 48 ~DtorDefaulted() = default; 49 }; 50 struct DtorDeleted { 51 ~DtorDeleted() = delete; 52 }; 53 54 struct Noexcept { 55 ~Noexcept() noexcept; 56 }; 57 struct NoexceptTrue { 58 ~NoexceptTrue() noexcept(true); 59 }; 60 struct NoexceptFalse { 61 ~NoexceptFalse() noexcept(false); 62 }; 63 64 struct CtorProtected { 65 protected: 66 CtorProtected() = default; 67 }; 68 struct CtorPrivate { 69 private: 70 CtorPrivate() = default; 71 }; 72 struct DtorProtected { 73 protected: 74 ~DtorProtected() = default; 75 }; 76 struct DtorPrivate { 77 private: 78 ~DtorPrivate() = default; 79 }; 80 81 template <class T> 82 struct NoexceptDependant { 83 ~NoexceptDependant() noexcept(std::is_same_v<T, int>); 84 }; 85 86 struct CtorExplicit { 87 explicit CtorExplicit() = default; 88 }; 89 struct CtorArgument { 90 CtorArgument(int) {} 91 }; 92 struct CtorDefaultArgument { 93 CtorDefaultArgument(int = 0) {} 94 }; 95 struct CtorExplicitDefaultArgument { 96 explicit CtorExplicitDefaultArgument(int = 0) {} 97 }; 98 99 struct Derived : public Empty {}; 100 101 class Abstract { 102 virtual void foo() = 0; 103 }; 104 105 class AbstractDestructor { 106 virtual ~AbstractDestructor() = 0; 107 }; 108 109 class OperatorNewDeleted { 110 void* operator new(std::size_t) = delete; 111 void operator delete(void* ptr) = delete; 112 }; 113 114 [[maybe_unused]] auto Lambda = [](const int&, int&&, double){}; 115 116 template<class T> 117 void test_not_const() 118 { 119 static_assert( std::default_initializable< T>); 120 static_assert(!std::default_initializable<const T>); 121 static_assert( std::default_initializable< volatile T>); 122 static_assert(!std::default_initializable<const volatile T>); 123 } 124 125 template<class T> 126 void test_true() 127 { 128 static_assert( std::default_initializable< T>); 129 static_assert( std::default_initializable<const T>); 130 static_assert( std::default_initializable< volatile T>); 131 static_assert( std::default_initializable<const volatile T>); 132 } 133 134 template<class T> 135 void test_false() 136 { 137 static_assert(!std::default_initializable< T>); 138 static_assert(!std::default_initializable<const T>); 139 static_assert(!std::default_initializable< volatile T>); 140 static_assert(!std::default_initializable<const volatile T>); 141 } 142 143 void test() 144 { 145 test_not_const<bool>(); 146 test_not_const<char>(); 147 test_not_const<int>(); 148 test_not_const<double>(); 149 150 test_false <void>(); 151 test_not_const<void*>(); 152 153 test_not_const<int*>(); 154 test_false <int[]>(); 155 test_not_const<int[1]>(); 156 test_false <int&>(); 157 test_false <int&&>(); 158 159 test_true <Empty>(); 160 161 test_true <CtorDefaulted>(); 162 test_false <CtorDeleted>(); 163 test_true <DtorDefaulted>(); 164 test_false <DtorDeleted>(); 165 166 test_true <Noexcept>(); 167 test_true <NoexceptTrue>(); 168 test_false <NoexceptFalse>(); 169 170 test_false <CtorProtected>(); 171 test_false <CtorPrivate>(); 172 test_false <DtorProtected>(); 173 test_false <DtorPrivate>(); 174 175 test_true <NoexceptDependant<int>>(); 176 test_false <NoexceptDependant<double>>(); 177 178 test_true <CtorExplicit>(); 179 test_false <CtorArgument>(); 180 test_true <CtorDefaultArgument>(); 181 test_true <CtorExplicitDefaultArgument>(); 182 183 test_true <Derived>(); 184 test_false <Abstract>(); 185 test_false <AbstractDestructor>(); 186 187 test_true <OperatorNewDeleted>(); 188 189 test_true <decltype(Lambda)>(); 190 test_not_const<void(*)(const int&)>(); 191 test_not_const<void(Empty::*)(const int&) >(); 192 test_not_const<void(Empty::*)(const int&) const >(); 193 test_not_const<void(Empty::*)(const int&) volatile>(); 194 test_not_const<void(Empty::*)(const int&) const volatile>(); 195 test_not_const<void(Empty::*)(const int&) &>(); 196 test_not_const<void(Empty::*)(const int&) &&>(); 197 test_not_const<void(Empty::*)(const int&) noexcept>(); 198 test_not_const<void(Empty::*)(const int&) noexcept(true)>(); 199 test_not_const<void(Empty::*)(const int&) noexcept(false)>(); 200 201 // Sequence containers 202 test_true <std::array< int, 0>>(); 203 test_not_const<std::array< int, 1>>(); 204 test_false <std::array<const int, 1>>(); 205 test_not_const<std::array< volatile int, 1>>(); 206 test_false <std::array<const volatile int, 1>>(); 207 test_true <std::deque< int>>(); 208 #ifdef _LIBCPP_VERSION 209 test_true <std::deque<const int>>(); 210 #endif // _LIBCPP_VERSION 211 test_true <std::forward_list<int>>(); 212 test_true <std::list<int>>(); 213 test_true <std::vector<int>>(); 214 215 // Associative containers 216 test_true <std::set<int>>(); 217 test_true <std::map<int, int>>(); 218 test_true <std::multiset<int>>(); 219 test_true <std::multimap<int, int>>(); 220 221 // Unordered associative containers 222 test_true <std::unordered_set<int>>(); 223 test_true <std::unordered_map<int, int>>(); 224 test_true <std::unordered_multiset<int>>(); 225 test_true <std::unordered_multimap<int, int>>(); 226 227 // Container adaptors 228 test_true <std::stack< int>>(); 229 #ifdef _LIBCPP_VERSION 230 test_true <std::stack<const int>>(); 231 #endif // _LIBCPP_VERSION 232 test_true <std::queue<int>>(); 233 test_true <std::priority_queue<int>>(); 234 235 test_true <std::span< int>>(); 236 test_true <std::span<const int>>(); 237 test_true <std::span< volatile int>>(); 238 test_true <std::span<const volatile int>>(); 239 240 // Strings 241 test_true <std::string>(); 242 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 243 test_true <std::wstring>(); 244 #endif 245 test_true <std::u8string>(); 246 test_true <std::u16string>(); 247 test_true <std::u32string>(); 248 249 // String views 250 test_true <std::string_view>(); 251 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 252 test_true <std::wstring_view>(); 253 #endif 254 test_true <std::u8string_view>(); 255 test_true <std::u16string_view>(); 256 test_true <std::u32string_view>(); 257 258 // Smart pointers 259 test_true <std::unique_ptr<int>>(); 260 test_true <std::shared_ptr<int>>(); 261 test_true <std::weak_ptr<int>>(); 262 263 } 264