]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/SemaTemplate/instantiate-exception-spec-cxx11.cpp
Vendor import of clang release_34 branch r197841 (effectively, 3.4 RC3):
[FreeBSD/FreeBSD.git] / test / SemaTemplate / instantiate-exception-spec-cxx11.cpp
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
2
3 // DR1330: an exception specification for a function template is only
4 // instantiated when it is needed.
5
6 template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
7 struct Incomplete; // expected-note{{forward}}
8
9 void test_f1(Incomplete *incomplete_p, int *int_p) {
10   f1(int_p);
11   f1(incomplete_p); // expected-note{{instantiation of exception spec}}
12 }
13
14 template<typename T> struct A {
15   template<typename U> struct B {
16     static void f() noexcept(A<U>().n);
17   };
18
19   constexpr A() : n(true) {}
20   bool n;
21 };
22
23 static_assert(noexcept(A<int>::B<char>::f()), "");
24
25 template<unsigned N> struct S {
26   static void recurse() noexcept(noexcept(S<N+1>::recurse())); // \
27   // expected-error {{no member named 'recurse'}} \
28   // expected-note 9{{instantiation of exception spec}}
29 };
30 decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
31 decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
32
33 template<> struct S<10> {};
34 void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
35
36
37 namespace dr1330_example {
38   template <class T> struct A {
39     void f(...) throw (typename T::X); // expected-error {{'int'}}
40     void f(int);
41   };
42
43   int main() {
44     A<int>().f(42);
45   }
46
47   struct S {
48     template<typename T>
49     static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
50     // expected-note {{instantiation of exception spec}}
51     typedef decltype(f<S>()) X;
52   };
53
54   int test2() {
55     S().f<S>(); // ok
56     S().f<int>(); // expected-note {{instantiation of exception spec}}
57   }
58 }
59
60 namespace core_19754_example {
61   template<typename T> T declval() noexcept;
62
63   template<typename T, typename = decltype(T(declval<T&&>()))>
64   struct is_movable { static const bool value = true; };
65
66   template<typename T>
67   struct wrap {
68     T val;
69     void irrelevant(wrap &p) noexcept(is_movable<T>::value);
70   };
71
72   template<typename T>
73   struct base {
74      base() {}
75      base(const typename T::type1 &);
76      base(const typename T::type2 &);
77   };
78
79   template<typename T>
80   struct type1 {
81      wrap<typename T::base> base;
82   };
83
84   template<typename T>
85   struct type2 {
86      wrap<typename T::base> base;
87   };
88
89   struct types {
90      typedef base<types> base;
91      typedef type1<types> type1;
92      typedef type2<types> type2;
93   };
94
95   base<types> val = base<types>();
96 }
97
98 namespace pr9485 {
99   template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
100   template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
101
102   template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
103   template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
104
105   void test() {
106     f1(0); // expected-error {{ambiguous}}
107     f2(0); // expected-error {{ambiguous}}
108   }
109 }
110
111 struct Exc1 { char c[4]; };
112 struct Exc2 { double x, y, z; };
113 struct Base {
114   virtual void f() noexcept; // expected-note {{overridden}}
115 };
116 template<typename T> struct Derived : Base {
117   void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
118   void g() noexcept (T::error);
119 };
120
121 Derived<Exc1> d1; // ok
122 Derived<Exc2> d2; // expected-note {{in instantiation of}}
123
124 // If the vtable for a derived class is used, the exception specification of
125 // any member function which ends up in that vtable is needed, even if it was
126 // declared in a base class.
127 namespace PR12763 {
128   template<bool *B> struct T {
129     virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
130   };
131   bool b; // expected-note {{here}}
132   struct X : public T<&b> {
133     virtual void g();
134   };
135   void X::g() {} // expected-note {{in instantiation of}}
136 }