blob: 97bf00303bbe938c247a25bdda9d526068300d27 [file] [log] [blame]
Richard Smith13bffc52012-04-19 00:08:28 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
Richard Smithe6975e92012-04-17 00:58:00 +00002
3// DR1330: an exception specification for a function template is only
4// instantiated when it is needed.
5
6template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
7struct Incomplete; // expected-note{{forward}}
8
9void test_f1(Incomplete *incomplete_p, int *int_p) {
10 f1(int_p);
11 f1(incomplete_p); // expected-note{{instantiation of exception spec}}
12}
13
14template<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
23static_assert(noexcept(A<int>::B<char>::f()), "");
24
25template<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};
30decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
31decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
32
33template<> struct S<10> {};
Richard Smith13bffc52012-04-19 00:08:28 +000034void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
Richard Smithe6975e92012-04-17 00:58:00 +000035
36
Richard Smithe6975e92012-04-17 00:58:00 +000037namespace 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 int test2() {
48 struct S {
49 template<typename T>
50 static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
51 // expected-note {{instantiation of exception spec}}
52 typedef decltype(f<S>()) X;
53 };
54 S().f<S>(); // ok
55 S().f<int>(); // expected-note {{instantiation of exception spec}}
56 }
57}
58
59namespace core_19754_example {
60 template<typename T> T declval() noexcept;
61
62 template<typename T, typename = decltype(T(declval<T&&>()))>
63 struct is_movable { static const bool value = true; };
64
65 template<typename T>
66 struct wrap {
67 T val;
68 void irrelevant(wrap &p) noexcept(is_movable<T>::value);
69 };
70
71 template<typename T>
72 struct base {
73 base() {}
74 base(const typename T::type1 &);
75 base(const typename T::type2 &);
76 };
77
78 template<typename T>
79 struct type1 {
80 wrap<typename T::base> base;
81 };
82
83 template<typename T>
84 struct type2 {
85 wrap<typename T::base> base;
86 };
87
88 struct types {
89 typedef base<types> base;
90 typedef type1<types> type1;
91 typedef type2<types> type2;
92 };
93
94 base<types> val = base<types>();
95}
Richard Smith87162c22012-04-17 22:30:01 +000096
97namespace pr9485 {
98 template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
99 template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
100
101 template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
102 template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
103
104 void test() {
105 f1(0); // expected-error {{ambiguous}}
106 f2(0); // expected-error {{ambiguous}}
107 }
108}
Richard Smith13bffc52012-04-19 00:08:28 +0000109
110struct Exc1 { char c[4]; };
111struct Exc2 { double x, y, z; };
112struct Base {
113 virtual void f() noexcept; // expected-note {{overridden}}
114};
115template<typename T> struct Derived : Base {
116 void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
117 void g() noexcept (T::error);
118};
119
120Derived<Exc1> d1; // ok
121Derived<Exc2> d2; // expected-note {{in instantiation of}}
Richard Smithff817f72012-07-07 06:59:51 +0000122
123// If the vtable for a derived class is used, the exception specification of
124// any member function which ends up in that vtable is needed, even if it was
125// declared in a base class.
126namespace PR12763 {
127 template<bool *B> struct T {
128 virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
129 };
130 bool b; // expected-note {{here}}
131 struct X : public T<&b> {
132 virtual void g();
133 };
134 void X::g() {} // expected-note {{in instantiation of}}
135}