xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx0x-defaulted-functions.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc void fn() = default; // expected-error {{only special member}}
4*f4a2713aSLionel Sambuc struct foo {
5*f4a2713aSLionel Sambuc   void fn() = default; // expected-error {{only special member}}
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc   foo() = default;
8*f4a2713aSLionel Sambuc   foo(const foo&) = default;
9*f4a2713aSLionel Sambuc   foo(foo&&) = default;
10*f4a2713aSLionel Sambuc   foo& operator = (const foo&) = default;
11*f4a2713aSLionel Sambuc   foo& operator = (foo&&) = default;
12*f4a2713aSLionel Sambuc   ~foo() = default;
13*f4a2713aSLionel Sambuc };
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc struct bar {
16*f4a2713aSLionel Sambuc   bar();
17*f4a2713aSLionel Sambuc   bar(const bar&);
18*f4a2713aSLionel Sambuc   bar(bar&&);
19*f4a2713aSLionel Sambuc   bar& operator = (const bar&);
20*f4a2713aSLionel Sambuc   bar& operator = (bar&&);
21*f4a2713aSLionel Sambuc   ~bar();
22*f4a2713aSLionel Sambuc };
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc bar::bar() = default;
25*f4a2713aSLionel Sambuc bar::bar(const bar&) = default;
26*f4a2713aSLionel Sambuc bar::bar(bar&&) = default;
27*f4a2713aSLionel Sambuc bar& bar::operator = (const bar&) = default;
28*f4a2713aSLionel Sambuc bar& bar::operator = (bar&&) = default;
29*f4a2713aSLionel Sambuc bar::~bar() = default;
30*f4a2713aSLionel Sambuc 
31*f4a2713aSLionel Sambuc static_assert(__is_trivial(foo), "foo should be trivial");
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc static_assert(!__has_trivial_destructor(bar), "bar's destructor isn't trivial");
34*f4a2713aSLionel Sambuc static_assert(!__has_trivial_constructor(bar),
35*f4a2713aSLionel Sambuc               "bar's default constructor isn't trivial");
36*f4a2713aSLionel Sambuc static_assert(!__has_trivial_copy(bar), "bar has no trivial copy");
37*f4a2713aSLionel Sambuc static_assert(!__has_trivial_assign(bar), "bar has no trivial assign");
38*f4a2713aSLionel Sambuc 
tester()39*f4a2713aSLionel Sambuc void tester() {
40*f4a2713aSLionel Sambuc   foo f, g(f);
41*f4a2713aSLionel Sambuc   bar b, c(b);
42*f4a2713aSLionel Sambuc   f = g;
43*f4a2713aSLionel Sambuc   b = c;
44*f4a2713aSLionel Sambuc }
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc template<typename T> struct S : T {
47*f4a2713aSLionel Sambuc   constexpr S() = default;
48*f4a2713aSLionel Sambuc   constexpr S(const S&) = default;
49*f4a2713aSLionel Sambuc   constexpr S(S&&) = default;
50*f4a2713aSLionel Sambuc };
litlit51*f4a2713aSLionel Sambuc struct lit { constexpr lit() {} };
52*f4a2713aSLionel Sambuc S<lit> s_lit; // ok
53*f4a2713aSLionel Sambuc S<bar> s_bar; // ok
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc struct Friends {
56*f4a2713aSLionel Sambuc   friend S<bar>::S();
57*f4a2713aSLionel Sambuc   friend S<bar>::S(const S&);
58*f4a2713aSLionel Sambuc   friend S<bar>::S(S&&);
59*f4a2713aSLionel Sambuc };
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc namespace DefaultedFnExceptionSpec {
62*f4a2713aSLionel Sambuc   // DR1330: The exception-specification of an implicitly-declared special
63*f4a2713aSLionel Sambuc   // member function is evaluated as needed.
64*f4a2713aSLionel Sambuc   template<typename T> T &&declval();
65*f4a2713aSLionel Sambuc   template<typename T> struct pair {
66*f4a2713aSLionel Sambuc     pair(const pair&) noexcept(noexcept(T(declval<T>())));
67*f4a2713aSLionel Sambuc   };
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc   struct Y;
70*f4a2713aSLionel Sambuc   struct X { X(); X(const Y&); };
71*f4a2713aSLionel Sambuc   struct Y { pair<X> p; };
72*f4a2713aSLionel Sambuc 
73*f4a2713aSLionel Sambuc   template<typename T>
74*f4a2713aSLionel Sambuc   struct A {
75*f4a2713aSLionel Sambuc     pair<T> p;
76*f4a2713aSLionel Sambuc   };
77*f4a2713aSLionel Sambuc   struct B {
78*f4a2713aSLionel Sambuc     B();
79*f4a2713aSLionel Sambuc     B(const A<B>&);
80*f4a2713aSLionel Sambuc   };
81*f4a2713aSLionel Sambuc 
82*f4a2713aSLionel Sambuc   // Don't crash here.
f()83*f4a2713aSLionel Sambuc   void f() {
84*f4a2713aSLionel Sambuc     X x = X();
85*f4a2713aSLionel Sambuc     (void)noexcept(B(declval<B>()));
86*f4a2713aSLionel Sambuc   }
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc   template<typename T>
89*f4a2713aSLionel Sambuc   struct Error {
90*f4a2713aSLionel Sambuc     // FIXME: Type canonicalization causes all the errors to point at the first
91*f4a2713aSLionel Sambuc     // declaration which has the type 'void () noexcept (T::error)'. We should
92*f4a2713aSLionel Sambuc     // get one error for 'Error<int>::Error()' and one for 'Error<int>::~Error()'.
93*f4a2713aSLionel Sambuc     void f() noexcept(T::error); // expected-error 2{{has no members}}
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc     Error() noexcept(T::error);
96*f4a2713aSLionel Sambuc     Error(const Error&) noexcept(T::error);
97*f4a2713aSLionel Sambuc     Error(Error&&) noexcept(T::error);
98*f4a2713aSLionel Sambuc     Error &operator=(const Error&) noexcept(T::error);
99*f4a2713aSLionel Sambuc     Error &operator=(Error&&) noexcept(T::error);
100*f4a2713aSLionel Sambuc     ~Error() noexcept(T::error);
101*f4a2713aSLionel Sambuc   };
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc   struct DelayImplicit {
104*f4a2713aSLionel Sambuc     Error<int> e;
105*f4a2713aSLionel Sambuc   };
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc   // Don't instantiate the exception specification here.
108*f4a2713aSLionel Sambuc   void test1(decltype(declval<DelayImplicit>() = DelayImplicit(DelayImplicit())));
109*f4a2713aSLionel Sambuc   void test2(decltype(declval<DelayImplicit>() = declval<const DelayImplicit>()));
110*f4a2713aSLionel Sambuc   void test3(decltype(DelayImplicit(declval<const DelayImplicit>())));
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc   // Any odr-use causes the exception specification to be evaluated.
113*f4a2713aSLionel Sambuc   struct OdrUse { // \
114*f4a2713aSLionel Sambuc     expected-note {{instantiation of exception specification for 'Error'}} \
115*f4a2713aSLionel Sambuc     expected-note {{instantiation of exception specification for '~Error'}}
116*f4a2713aSLionel Sambuc     Error<int> e;
117*f4a2713aSLionel Sambuc   };
118*f4a2713aSLionel Sambuc   OdrUse use; // expected-note {{implicit default constructor for 'DefaultedFnExceptionSpec::OdrUse' first required here}}
119*f4a2713aSLionel Sambuc }
120*f4a2713aSLionel Sambuc 
121*f4a2713aSLionel Sambuc namespace PR13527 {
122*f4a2713aSLionel Sambuc   struct X {
123*f4a2713aSLionel Sambuc     X() = delete; // expected-note {{here}}
124*f4a2713aSLionel Sambuc     X(const X&) = delete; // expected-note {{here}}
125*f4a2713aSLionel Sambuc     X(X&&) = delete; // expected-note {{here}}
126*f4a2713aSLionel Sambuc     X &operator=(const X&) = delete; // expected-note {{here}}
127*f4a2713aSLionel Sambuc     X &operator=(X&&) = delete; // expected-note {{here}}
128*f4a2713aSLionel Sambuc     ~X() = delete; // expected-note {{here}}
129*f4a2713aSLionel Sambuc   };
130*f4a2713aSLionel Sambuc   X::X() = default; // expected-error {{redefinition}}
131*f4a2713aSLionel Sambuc   X::X(const X&) = default; // expected-error {{redefinition}}
132*f4a2713aSLionel Sambuc   X::X(X&&) = default; // expected-error {{redefinition}}
133*f4a2713aSLionel Sambuc   X &X::operator=(const X&) = default; // expected-error {{redefinition}}
134*f4a2713aSLionel Sambuc   X &X::operator=(X&&) = default; // expected-error {{redefinition}}
135*f4a2713aSLionel Sambuc   X::~X() = default; // expected-error {{redefinition}}
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc   struct Y {
138*f4a2713aSLionel Sambuc     Y() = default;
139*f4a2713aSLionel Sambuc     Y(const Y&) = default;
140*f4a2713aSLionel Sambuc     Y(Y&&) = default;
141*f4a2713aSLionel Sambuc     Y &operator=(const Y&) = default;
142*f4a2713aSLionel Sambuc     Y &operator=(Y&&) = default;
143*f4a2713aSLionel Sambuc     ~Y() = default;
144*f4a2713aSLionel Sambuc   };
145*f4a2713aSLionel Sambuc   Y::Y() = default; // expected-error {{definition of explicitly defaulted}}
146*f4a2713aSLionel Sambuc   Y::Y(const Y&) = default; // expected-error {{definition of explicitly defaulted}}
147*f4a2713aSLionel Sambuc   Y::Y(Y&&) = default; // expected-error {{definition of explicitly defaulted}}
148*f4a2713aSLionel Sambuc   Y &Y::operator=(const Y&) = default; // expected-error {{definition of explicitly defaulted}}
149*f4a2713aSLionel Sambuc   Y &Y::operator=(Y&&) = default; // expected-error {{definition of explicitly defaulted}}
150*f4a2713aSLionel Sambuc   Y::~Y() = default; // expected-error {{definition of explicitly defaulted}}
151*f4a2713aSLionel Sambuc }
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc namespace PR14577 {
154*f4a2713aSLionel Sambuc   template<typename T>
155*f4a2713aSLionel Sambuc   struct Outer {
156*f4a2713aSLionel Sambuc     template<typename U>
157*f4a2713aSLionel Sambuc     struct Inner1 {
158*f4a2713aSLionel Sambuc       ~Inner1();
159*f4a2713aSLionel Sambuc     };
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc     template<typename U>
162*f4a2713aSLionel Sambuc     struct Inner2 {
163*f4a2713aSLionel Sambuc       ~Inner2();
164*f4a2713aSLionel Sambuc     };
165*f4a2713aSLionel Sambuc   };
166*f4a2713aSLionel Sambuc 
167*f4a2713aSLionel Sambuc   template<typename T>
168*f4a2713aSLionel Sambuc   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}}
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc   template<typename T>
171*f4a2713aSLionel Sambuc   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}}  expected-error {{only special member functions may be defaulted}}
172*f4a2713aSLionel Sambuc }
173*f4a2713aSLionel Sambuc 
174*f4a2713aSLionel Sambuc extern "C" {
175*f4a2713aSLionel Sambuc  template<typename _Tp> // expected-error {{templates must have C++ linkage}}
176*f4a2713aSLionel Sambuc  void PR13573(const _Tp&) = delete; // expected-error {{only functions can have deleted definitions}}
177*f4a2713aSLionel Sambuc }
178*f4a2713aSLionel Sambuc 
179*f4a2713aSLionel Sambuc namespace PR15597 {
180*f4a2713aSLionel Sambuc   template<typename T> struct A {
181*f4a2713aSLionel Sambuc     A() noexcept(true) = default;
182*f4a2713aSLionel Sambuc     ~A() noexcept(true) = default;
183*f4a2713aSLionel Sambuc   };
184*f4a2713aSLionel Sambuc   template<typename T> struct B {
185*f4a2713aSLionel Sambuc     B() noexcept(false) = default; // expected-error {{does not match the calculated one}}
186*f4a2713aSLionel Sambuc     ~B() noexcept(false) = default; // expected-error {{does not match the calculated one}}
187*f4a2713aSLionel Sambuc   };
188*f4a2713aSLionel Sambuc   A<int> a;
189*f4a2713aSLionel Sambuc   B<int> b; // expected-note {{here}}
190*f4a2713aSLionel Sambuc }
191