1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // type_traits
11
12 // is_default_constructible
13
14 #include <type_traits>
15 #include "test_macros.h"
16
17 template <class T>
test_is_default_constructible()18 void test_is_default_constructible()
19 {
20 static_assert( std::is_default_constructible<T>::value, "");
21 static_assert( std::is_default_constructible<const T>::value, "");
22 static_assert( std::is_default_constructible<volatile T>::value, "");
23 static_assert( std::is_default_constructible<const volatile T>::value, "");
24 #if TEST_STD_VER > 14
25 static_assert( std::is_default_constructible_v<T>, "");
26 static_assert( std::is_default_constructible_v<const T>, "");
27 static_assert( std::is_default_constructible_v<volatile T>, "");
28 static_assert( std::is_default_constructible_v<const volatile T>, "");
29 #endif
30 }
31
32 template <class T>
test_is_not_default_constructible()33 void test_is_not_default_constructible()
34 {
35 static_assert(!std::is_default_constructible<T>::value, "");
36 static_assert(!std::is_default_constructible<const T>::value, "");
37 static_assert(!std::is_default_constructible<volatile T>::value, "");
38 static_assert(!std::is_default_constructible<const volatile T>::value, "");
39 #if TEST_STD_VER > 14
40 static_assert(!std::is_default_constructible_v<T>, "");
41 static_assert(!std::is_default_constructible_v<const T>, "");
42 static_assert(!std::is_default_constructible_v<volatile T>, "");
43 static_assert(!std::is_default_constructible_v<const volatile T>, "");
44 #endif
45 }
46
47 class Empty
48 {
49 };
50
51 class NoDefaultConstructor
52 {
NoDefaultConstructor(int)53 NoDefaultConstructor(int) {}
54 };
55
56 class NotEmpty
57 {
58 public:
59 virtual ~NotEmpty();
60 };
61
62 union Union {};
63
64 struct bit_zero
65 {
66 int : 0;
67 };
68
69 class Abstract
70 {
71 public:
72 virtual ~Abstract() = 0;
73 };
74
75 struct A
76 {
77 A();
78 };
79
80 class B
81 {
82 B();
83 };
84
main()85 int main()
86 {
87 test_is_default_constructible<A>();
88 test_is_default_constructible<Union>();
89 test_is_default_constructible<Empty>();
90 test_is_default_constructible<int>();
91 test_is_default_constructible<double>();
92 test_is_default_constructible<int*>();
93 test_is_default_constructible<const int*>();
94 test_is_default_constructible<char[3]>();
95 test_is_default_constructible<char[5][3]>();
96
97 test_is_default_constructible<NotEmpty>();
98 test_is_default_constructible<bit_zero>();
99
100 test_is_not_default_constructible<void>();
101 test_is_not_default_constructible<int&>();
102 test_is_not_default_constructible<char[]>();
103 test_is_not_default_constructible<char[][3]>();
104
105 test_is_not_default_constructible<Abstract>();
106 test_is_not_default_constructible<NoDefaultConstructor>();
107 #if TEST_STD_VER >= 11
108 test_is_not_default_constructible<B>();
109 test_is_not_default_constructible<int&&>();
110
111 // TODO: Remove this workaround once Clang <= 3.7 are no longer used regularly.
112 // In those compiler versions the __is_constructible builtin gives the wrong
113 // results for abominable function types.
114 #if (defined(TEST_APPLE_CLANG_VER) && TEST_APPLE_CLANG_VER < 703) \
115 || (defined(TEST_CLANG_VER) && TEST_CLANG_VER < 308)
116 #define WORKAROUND_CLANG_BUG
117 #endif
118 #if !defined(WORKAROUND_CLANG_BUG)
119 test_is_not_default_constructible<void()>();
120 test_is_not_default_constructible<void() const> ();
121 test_is_not_default_constructible<void() volatile> ();
122 test_is_not_default_constructible<void() &> ();
123 test_is_not_default_constructible<void() &&> ();
124 #endif
125 #endif
126 }
127