• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 
3 struct non_copiable {
4   non_copiable(const non_copiable&) = delete; // expected-note {{marked deleted here}}
5   non_copiable& operator = (const non_copiable&) = delete; // expected-note {{explicitly deleted}}
6   non_copiable() = default;
7 };
8 
9 struct non_const_copy {
10   non_const_copy(non_const_copy&);
11   non_const_copy& operator = (non_const_copy&) &;
12   non_const_copy& operator = (non_const_copy&) &&;
13   non_const_copy() = default; // expected-note {{not viable}}
14   int uninit_field;
15 };
16 non_const_copy::non_const_copy(non_const_copy&) = default; // expected-note {{not viable}}
17 non_const_copy& non_const_copy::operator = (non_const_copy&) & = default; // expected-note {{not viable}}
18 non_const_copy& non_const_copy::operator = (non_const_copy&) && = default; // expected-note {{not viable}}
19 
fn1()20 void fn1 () {
21   non_copiable nc;
22   non_copiable nc2 = nc; // expected-error {{deleted constructor}}
23   nc = nc; // expected-error {{deleted operator}}
24 
25   non_const_copy ncc;
26   non_const_copy ncc2 = ncc;
27   ncc = ncc2;
28   const non_const_copy cncc{};
29   const non_const_copy cncc1; // expected-error {{default initialization of an object of const type 'const non_const_copy' without a user-provided default constructor}}
30   non_const_copy ncc3 = cncc; // expected-error {{no matching}}
31   ncc = cncc; // expected-error {{no viable overloaded}}
32 };
33 
34 struct no_fields { };
35 struct all_init {
36   int a = 0;
37   int b = 0;
38 };
39 struct some_init {
40   int a = 0;
41   int b;
42   int c = 0;
43 };
44 struct some_init_mutable {
45   int a = 0;
46   mutable int b;
47   int c = 0;
48 };
49 struct some_init_def {
50   some_init_def() = default;
51   int a = 0;
52   int b;
53   int c = 0;
54 };
55 struct some_init_ctor {
56   some_init_ctor();
57   int a = 0;
58   int b;
59   int c = 0;
60 };
61 struct sub_some_init : public some_init_def { };
62 struct sub_some_init_ctor : public some_init_def {
63   sub_some_init_ctor();
64 };
65 struct sub_some_init_ctor2 : public some_init_ctor {
66 };
67 struct some_init_container {
68   some_init_def sid;
69 };
70 struct some_init_container_ctor {
71   some_init_container_ctor();
72   some_init_def sid;
73 };
74 struct no_fields_container {
75   no_fields nf;
76 };
77 struct param_pack_ctor {
78   template <typename... T>
79   param_pack_ctor(T...);
80   int n;
81 };
82 struct param_pack_ctor_field {
83   param_pack_ctor ndc;
84 };
85 struct multi_param_pack_ctor {
86   template <typename... T, typename... U>
87   multi_param_pack_ctor(T..., U..., int f = 0);
88   int n;
89 };
90 struct ignored_template_ctor_and_def {
91   template <class T> ignored_template_ctor_and_def(T* f = nullptr);
92   ignored_template_ctor_and_def() = default;
93   int field;
94 };
95 template<bool, typename = void> struct enable_if {};
96 template<typename T> struct enable_if<true, T> { typedef T type; };
97 struct multi_param_pack_and_defaulted {
98   template <typename... T,
99             typename enable_if<sizeof...(T) != 0>::type* = nullptr>
100   multi_param_pack_and_defaulted(T...);
101   multi_param_pack_and_defaulted() = default;
102   int n;
103 };
104 
constobjs()105 void constobjs() {
106   const no_fields nf; // ok
107   const all_init ai; // ok
108   const some_init si; // expected-error {{default initialization of an object of const type 'const some_init' without a user-provided default constructor}}
109   const some_init_mutable sim; // ok
110   const some_init_def sid; // expected-error {{default initialization of an object of const type 'const some_init_def' without a user-provided default constructor}}
111   const some_init_ctor sic; // ok
112   const sub_some_init ssi; // expected-error {{default initialization of an object of const type 'const sub_some_init' without a user-provided default constructor}}
113   const sub_some_init_ctor ssic; // ok
114   const sub_some_init_ctor2 ssic2; // ok
115   const some_init_container sicon; // expected-error {{default initialization of an object of const type 'const some_init_container' without a user-provided default constructor}}
116   const some_init_container_ctor siconc; // ok
117   const no_fields_container nfc; // ok
118   const param_pack_ctor ppc; // ok
119   const param_pack_ctor_field ppcf; // ok
120   const multi_param_pack_ctor mppc; // ok
121   const multi_param_pack_and_defaulted mppad; // expected-error {{default initialization of an object of const type 'const multi_param_pack_and_defaulted' without a user-provided default constructor}}
122   const ignored_template_ctor_and_def itcad; // expected-error {{default initialization of an object of const type 'const ignored_template_ctor_and_def' without a user-provided default constructor}}
123 
124 }
125 
126 struct non_const_derived : non_const_copy {
127   non_const_derived(const non_const_derived&) = default; // expected-error {{requires it to be non-const}}
128   non_const_derived& operator =(non_const_derived&) = default;
129 };
130 
131 struct bad_decls {
132   bad_decls(volatile bad_decls&) = default; // expected-error {{may not be volatile}}
133   bad_decls&& operator = (bad_decls) = default; // expected-error {{lvalue reference}} expected-error {{must return 'bad_decls &'}}
134   bad_decls& operator = (volatile bad_decls&) = default; // expected-error {{may not be volatile}}
135   bad_decls& operator = (const bad_decls&) const = default; // expected-error {{may not have 'const', 'constexpr' or 'volatile' qualifiers}}
136 };
137 
138 struct DefaultDelete {
139   DefaultDelete() = default; // expected-note {{previous declaration is here}}
140   DefaultDelete() = delete; // expected-error {{constructor cannot be redeclared}}
141 
142   ~DefaultDelete() = default; // expected-note {{previous declaration is here}}
143   ~DefaultDelete() = delete; // expected-error {{destructor cannot be redeclared}}
144 
145   DefaultDelete &operator=(const DefaultDelete &) = default; // expected-note {{previous declaration is here}}
146   DefaultDelete &operator=(const DefaultDelete &) = delete; // expected-error {{class member cannot be redeclared}}
147 };
148 
149 struct DeleteDefault {
150   DeleteDefault() = delete; // expected-note {{previous definition is here}}
151   DeleteDefault() = default; // expected-error {{constructor cannot be redeclared}}
152 
153   ~DeleteDefault() = delete; // expected-note {{previous definition is here}}
154   ~DeleteDefault() = default; // expected-error {{destructor cannot be redeclared}}
155 
156   DeleteDefault &operator=(const DeleteDefault &) = delete; // expected-note {{previous definition is here}}
157   DeleteDefault &operator=(const DeleteDefault &) = default; // expected-error {{class member cannot be redeclared}}
158 };
159 
160 struct A {}; struct B {};
161 
162 struct except_spec_a {
163   virtual ~except_spec_a() throw(A);
164   except_spec_a() throw(A);
165 };
166 struct except_spec_b {
167   virtual ~except_spec_b() throw(B);
168   except_spec_b() throw(B);
169 };
170 
171 struct except_spec_d_good : except_spec_a, except_spec_b {
172   ~except_spec_d_good();
173 };
174 except_spec_d_good::~except_spec_d_good() = default;
175 struct except_spec_d_good2 : except_spec_a, except_spec_b {
176   ~except_spec_d_good2() = default;
177 };
178 struct except_spec_d_bad : except_spec_a, except_spec_b {
179   ~except_spec_d_bad() noexcept;
180 };
181 // FIXME: This should error because this exception spec is not
182 // compatible with the implicit exception spec.
183 except_spec_d_bad::~except_spec_d_bad() noexcept = default;
184 
185 // FIXME: This should error because this exception spec is not
186 // compatible with the implicit exception spec.
187 struct except_spec_d_mismatch : except_spec_a, except_spec_b {
188   except_spec_d_mismatch() throw(A) = default;
189 };
190 struct except_spec_d_match : except_spec_a, except_spec_b {
191   except_spec_d_match() throw(A, B) = default;
192 };
193 
194 // gcc-compatibility: allow attributes on default definitions
195 // (but not normal definitions)
196 struct S { S(); };
197 S::S() __attribute((pure)) = default;
198 
199 using size_t = decltype(sizeof(0));
200 void *operator new(size_t) = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}
201 void operator delete(void *) noexcept = delete; // expected-error {{deleted definition must be first declaration}} expected-note {{implicit}}
202