[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / cxx0x-defaulted-functions.cpp
blob0c3dd1ea7aa274199fd0a56415a9ce9fcb3dbc4d
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions -Wno-deprecated-builtins %s
3 void fn() = default; // expected-error {{only special member}}
4 struct foo {
5 void fn() = default; // expected-error {{only special member}}
7 foo() = default;
8 foo(const foo&) = default;
9 foo(foo&&) = default;
10 foo& operator = (const foo&) = default;
11 foo& operator = (foo&&) = default;
12 ~foo() = default;
15 struct bar {
16 bar();
17 bar(const bar&);
18 bar(bar&&);
19 bar& operator = (const bar&);
20 bar& operator = (bar&&);
21 ~bar();
24 bar::bar() = default;
25 bar::bar(const bar&) = default;
26 bar::bar(bar&&) = default;
27 bar& bar::operator = (const bar&) = default;
28 bar& bar::operator = (bar&&) = default;
29 bar::~bar() = default;
31 static_assert(__is_trivial(foo), "foo should be trivial");
33 static_assert(!__has_trivial_destructor(bar), "bar's destructor isn't trivial");
34 static_assert(!__has_trivial_constructor(bar),
35 "bar's default constructor isn't trivial");
36 static_assert(!__has_trivial_copy(bar), "bar has no trivial copy");
37 static_assert(!__has_trivial_assign(bar), "bar has no trivial assign");
39 void tester() {
40 foo f, g(f);
41 bar b, c(b);
42 f = g;
43 b = c;
46 template<typename T> struct S : T {
47 constexpr S() = default; // expected-note {{previous declaration is here}}
48 constexpr S(const S&) = default; // expected-note {{previous declaration is here}}
49 constexpr S(S&&) = default; // expected-note {{previous declaration is here}}
51 struct lit { constexpr lit() {} };
52 S<lit> s_lit; // ok
53 S<bar> s_bar; // ok
55 struct Friends {
56 // FIXME: these error may or may not be correct; there is an open question on
57 // the CWG reflectors about this.
58 friend S<bar>::S(); // expected-error {{non-constexpr declaration of 'S' follows constexpr declaration}}
59 friend S<bar>::S(const S&); // expected-error {{non-constexpr declaration of 'S' follows constexpr declaration}}
60 friend S<bar>::S(S&&); // expected-error {{non-constexpr declaration of 'S' follows constexpr declaration}}
63 namespace DefaultedFnExceptionSpec {
64 // DR1330: The exception-specification of an implicitly-declared special
65 // member function is evaluated as needed.
66 template<typename T> T &&declval();
67 template<typename T> struct pair {
68 pair(const pair&) noexcept(noexcept(T(declval<T>())));
71 struct Y;
72 struct X { X(); X(const Y&); };
73 struct Y { pair<X> p; };
75 template<typename T>
76 struct A {
77 pair<T> p;
79 struct B {
80 B();
81 B(const A<B>&);
84 // Don't crash here.
85 void f() {
86 X x = X();
87 (void)noexcept(B(declval<B>()));
90 template<typename T>
91 struct Error {
92 void f() noexcept(T::error);
94 Error() noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} expected-error {{type 'char'}}
95 Error(const Error&) noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
96 Error(Error&&) noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
97 Error &operator=(const Error&) noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} expected-error {{type 'double'}}
98 Error &operator=(Error&&) noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
99 ~Error() noexcept(T::error); // expected-error {{type 'int' cannot be used prior to '::' because it has no members}} expected-error {{type 'char'}}
102 Error<char> c; // expected-note 2{{instantiation of}}
103 struct DelayImplicit {
104 Error<int> e; // expected-note 6{{instantiation of}}
106 Error<float> *e;
108 // An exception specification is needed if the exception specification for a
109 // a defaulted special member function that calls the function is needed.
110 // Use in an unevaluated operand still results in the exception spec being
111 // needed.
112 void test1(decltype(declval<DelayImplicit>() = DelayImplicit(DelayImplicit()))); // expected-note 4{{in evaluation of exception specification}}
113 void test2(decltype(declval<DelayImplicit>() = declval<const DelayImplicit>())); // expected-note {{in evaluation of exception specification}}
114 void test3(decltype(DelayImplicit(declval<const DelayImplicit>()))); // expected-note {{in evaluation of exception specification}}
116 // Any odr-use needs the exception specification.
117 void f(Error<double> *p) {
118 *p = *p; // expected-note {{instantiation of}}
122 namespace PR13527 {
123 struct X {
124 X() = delete; // expected-note {{here}}
125 X(const X&) = delete; // expected-note {{here}}
126 X(X&&) = delete; // expected-note {{here}}
127 X &operator=(const X&) = delete; // expected-note {{here}}
128 X &operator=(X&&) = delete; // expected-note {{here}}
129 ~X() = delete; // expected-note {{here}}
131 X::X() = default; // expected-error {{redefinition}}
132 X::X(const X&) = default; // expected-error {{redefinition}}
133 X::X(X&&) = default; // expected-error {{redefinition}}
134 X &X::operator=(const X&) = default; // expected-error {{redefinition}}
135 X &X::operator=(X&&) = default; // expected-error {{redefinition}}
136 X::~X() = default; // expected-error {{redefinition}}
138 struct Y {
139 Y() = default;
140 Y(const Y&) = default;
141 Y(Y&&) = default;
142 Y &operator=(const Y&) = default;
143 Y &operator=(Y&&) = default;
144 ~Y() = default;
146 Y::Y() noexcept = default; // expected-error {{definition of explicitly defaulted}}
147 Y::Y(const Y&) noexcept = default; // expected-error {{definition of explicitly defaulted}}
148 Y::Y(Y&&) noexcept = default; // expected-error {{definition of explicitly defaulted}}
149 Y &Y::operator=(const Y&) noexcept = default; // expected-error {{definition of explicitly defaulted}}
150 Y &Y::operator=(Y&&) noexcept = default; // expected-error {{definition of explicitly defaulted}}
151 Y::~Y() = default; // expected-error {{definition of explicitly defaulted}}
154 namespace PR27699 {
155 struct X {
156 X();
158 X::X() = default; // expected-note {{here}}
159 X::X() = default; // expected-error {{redefinition of 'X'}}
162 namespace PR14577 {
163 template<typename T>
164 struct Outer {
165 template<typename U>
166 struct Inner1 {
167 ~Inner1();
170 template<typename U>
171 struct Inner2 {
172 ~Inner2();
176 template<typename T>
177 Outer<T>::Inner1<T>::~Inner1() = delete; // expected-error {{nested name specifier 'Outer<T>::Inner1<T>::' for declaration does not refer into a class, class template or class template partial specialization}} expected-error {{only functions can have deleted definitions}}
179 template<typename T>
180 Outer<T>::Inner2<T>::~Inner2() = default; // expected-error {{nested name specifier 'Outer<T>::Inner2<T>::' for declaration does not refer into a class, class template or class template partial specialization}}
183 extern "C" { // expected-note {{extern "C" language linkage specification begins here}}
184 template<typename _Tp> // expected-error {{templates must have C++ linkage}}
185 void PR13573(const _Tp&) = delete;
188 namespace PR15597 {
189 template<typename T> struct A {
190 A() noexcept(true) = default;
191 ~A() noexcept(true) = default;
193 template<typename T> struct B {
194 B() noexcept(false) = default;
195 ~B() noexcept(false) = default;
197 A<int> a;
198 B<int> b;
201 namespace PR27941 {
202 struct ExplicitBool {
203 ExplicitBool &operator=(bool) = default; // expected-error{{only special member functions may be defaulted}}
204 int member;
207 int fn() {
208 ExplicitBool t;
209 t = true;
213 namespace dependent_classes {
214 template <bool B, typename X, typename Y>
215 struct conditional;
217 template <typename X, typename Y>
218 struct conditional<true, X, Y> { typedef X type; };
220 template <typename X, typename Y>
221 struct conditional<false, X, Y> { typedef Y type; };
223 template<bool B> struct X {
224 X();
226 // B == false triggers error for = default.
227 using T = typename conditional<B, const X &, int>::type;
228 X(T) = default; // expected-error {{only special member functions}}
230 // Either value of B creates a constructor that can be default
231 using U = typename conditional<B, X&&, const X&>::type;
232 X(U) = default;
235 X<true> x1;
236 X<false> x2; // expected-note {{in instantiation}}
238 template <typename Type>
239 class E {
240 explicit E(const int &) = default;
243 template <typename Type>
244 E<Type>::E(const int&) {} // expected-error {{definition of explicitly defaulted function}}
248 namespace P1286R2 {
249 struct X {
250 X();
252 struct A {
253 struct B {
254 B() noexcept(A::value) = default;
255 X x;
257 decltype(B()) b;
258 static constexpr bool value = true;
260 A::B b;
262 static_assert(noexcept(A::B()), "");
265 namespace GH56456 {
266 template <typename T>
267 using RC=T const&;
268 template <typename T>
269 using RV=T&;
270 template <typename T>
271 using RM=T&&;
273 struct A {
274 A(RC<A>) = default;
275 A(RM<A>) = default;
277 auto operator=(RC<A>) -> RV<A> = default;
278 auto operator=(RM<A>) -> RV<A> = default;
281 struct B {
282 B (RC<B>) = delete;
283 B (RM<B>) = delete;
285 auto operator = (RC<B>) -> RV<B> = delete;
286 auto operator = (RM<B>) -> RV<B> = delete;