258 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// We voluntarily use std::default_initializable on types that have redundant12// or ignored cv-qualifiers -- don't warn about it.13// ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-ignored-qualifiers14 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 39struct Empty {};40 41struct CtorDefaulted {42 CtorDefaulted() = default;43};44struct CtorDeleted {45 CtorDeleted() = delete;46};47struct DtorDefaulted {48 ~DtorDefaulted() = default;49};50struct DtorDeleted {51 ~DtorDeleted() = delete;52};53 54struct Noexcept {55 ~Noexcept() noexcept;56};57struct NoexceptTrue {58 ~NoexceptTrue() noexcept(true);59};60struct NoexceptFalse {61 ~NoexceptFalse() noexcept(false);62};63 64struct CtorProtected {65protected:66 CtorProtected() = default;67};68struct CtorPrivate {69private:70 CtorPrivate() = default;71};72struct DtorProtected {73protected:74 ~DtorProtected() = default;75};76struct DtorPrivate {77private:78 ~DtorPrivate() = default;79};80 81template <class T>82struct NoexceptDependant {83 ~NoexceptDependant() noexcept(std::is_same_v<T, int>);84};85 86struct CtorExplicit {87 explicit CtorExplicit() = default;88};89struct CtorArgument {90 CtorArgument(int) {}91};92struct CtorDefaultArgument {93 CtorDefaultArgument(int = 0) {}94};95struct CtorExplicitDefaultArgument {96 explicit CtorExplicitDefaultArgument(int = 0) {}97};98 99struct Derived : public Empty {};100 101class Abstract {102 virtual void foo() = 0;103};104 105class AbstractDestructor {106 virtual ~AbstractDestructor() = 0;107};108 109class 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 116template<class T>117void 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 125template<class T>126void 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 134template<class T>135void 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 143void 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 containers202 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 test_true <std::forward_list<int>>();209 test_true <std::list<int>>();210 test_true <std::vector<int>>();211 212 // Associative containers213 test_true <std::set<int>>();214 test_true <std::map<int, int>>();215 test_true <std::multiset<int>>();216 test_true <std::multimap<int, int>>();217 218 // Unordered associative containers219 test_true <std::unordered_set<int>>();220 test_true <std::unordered_map<int, int>>();221 test_true <std::unordered_multiset<int>>();222 test_true <std::unordered_multimap<int, int>>();223 224 // Container adaptors225 test_true <std::stack< int>>();226 test_true <std::queue<int>>();227 test_true <std::priority_queue<int>>();228 229 test_true <std::span< int>>();230 test_true <std::span<const int>>();231 test_true <std::span< volatile int>>();232 test_true <std::span<const volatile int>>();233 234 // Strings235 test_true <std::string>();236#ifndef TEST_HAS_NO_WIDE_CHARACTERS237 test_true <std::wstring>();238#endif239 test_true <std::u8string>();240 test_true <std::u16string>();241 test_true <std::u32string>();242 243 // String views244 test_true <std::string_view>();245#ifndef TEST_HAS_NO_WIDE_CHARACTERS246 test_true <std::wstring_view>();247#endif248 test_true <std::u8string_view>();249 test_true <std::u16string_view>();250 test_true <std::u32string_view>();251 252 // Smart pointers253 test_true <std::unique_ptr<int>>();254 test_true <std::shared_ptr<int>>();255 test_true <std::weak_ptr<int>>();256 257}258