Debian Codegraph @

gcc/testsuite/g++.dg/ext/has_nothrow_constructor.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } }
3 #include <cassert>
4
5 struct A
6 {
7 double a;
8 double b;
9 };
10
11 struct B
12 {
13 A a;
14 };
15
16 #if __cplusplus > 201402L
17 #define THROW_INT
18 #else
19 #define THROW_INT throw(int) // { dg-warning "deprecated" "" { target { c++11 && { ! c++17 } } } }
20 #endif
21
22 struct C
23 : public A { };
24
25 struct D
26 {
27 D() throw() { }
28 };
29
30 struct E
31 {
32 E() THROW_INT { }
33 };
34
35 struct E1
36 {
37 E1() THROW_INT { throw int(); }
38 };
39
40 struct F
41 {
42 F(const F&) throw() { }
43 };
44
45 struct G
46 {
47 G(const G&) THROW_INT { throw int(); }
48 };
49
50 template<typename T>
51 bool
52 f()
53 { return __has_nothrow_constructor(T); }
54
55 template<typename T>
56 class My
57 {
58 public:
59 bool
60 f()
61 { return !!__has_nothrow_constructor(T); }
62 };
63
64 template<typename T>
65 class My2
66 {
67 public:
68 static const bool trait = __has_nothrow_constructor(T);
69 };
70
71 template<typename T>
72 const bool My2<T>::trait;
73
74
75 template<typename T, bool b = __has_nothrow_constructor(T)>
76 struct My3_help
77 { static const bool trait = b; };
78
79 template<typename T, bool b>
80 const bool My3_help<T, b>::trait;
81
82 template<typename T>
83 class My3
84 {
85 public:
86 bool
87 f()
88 { return My3_help<T>::trait; }
89 };
90
91 #define PTEST(T) (__has_nothrow_constructor(T) && f<T>() \
92 && My<T>().f() && My2<T>::trait && My3<T>().f())
93
94 #define NTEST(T) (!__has_nothrow_constructor(T) && !f<T>() \
95 && !My<T>().f() && !My2<T>::trait && !My3<T>().f())
96
97 int main()
98 {
99 assert (PTEST (int));
100 assert (NTEST (int (int)));
101 assert (NTEST (void));
102 assert (PTEST (A));
103 assert (PTEST (B));
104 assert (PTEST (C));
105 assert (PTEST (C[]));
106 assert (PTEST (D));
107 assert (NTEST (E));
108 assert (NTEST (E1));
109 assert (NTEST (F));
110 assert (NTEST (G));
111
112 return 0;
113 }