blob: 5f43ea2c27a19ac5498a1b9ce8b1d47a3382af57 [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
David Majnemer588a51a2013-10-22 04:14:18 +000047 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
Richard Smithe6975e92012-04-17 00:58:00 +000054 int test2() {
Richard Smithe6975e92012-04-17 00:58:00 +000055 S().f<S>(); // ok
56 S().f<int>(); // expected-note {{instantiation of exception spec}}
57 }
58}
59
60namespace 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}
Richard Smith87162c22012-04-17 22:30:01 +000097
98namespace 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}
Richard Smith13bffc52012-04-19 00:08:28 +0000110
111struct Exc1 { char c[4]; };
112struct Exc2 { double x, y, z; };
113struct Base {
114 virtual void f() noexcept; // expected-note {{overridden}}
115};
116template<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
121Derived<Exc1> d1; // ok
122Derived<Exc2> d2; // expected-note {{in instantiation of}}
Richard Smithff817f72012-07-07 06:59:51 +0000123
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.
127namespace 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}