blob: a376f0e5fd5ba78373b45a7e020f89c87d6f1044 [file] [log] [blame]
Hans Wennborgc9bd88e2014-01-14 19:35:09 +00001// RUN: %clang_cc1 -fsyntax-only -verify -triple %itanium_abi_triple -std=c++11 -ftemplate-depth 16 -fcxx-exceptions -fexceptions %s
Richard Smithf623c962012-04-17 00:58:00 +00002
3// DR1330: an exception specification for a function template is only
4// instantiated when it is needed.
5
Hans Wennborg9125b082014-01-13 19:48:13 +00006// Note: the test is Itanium-specific because it depends on key functions in the
7// PR12763 namespace.
8
Richard Smithf623c962012-04-17 00:58:00 +00009template<typename T> void f1(T*) throw(T); // expected-error{{incomplete type 'Incomplete' is not allowed in exception specification}}
10struct Incomplete; // expected-note{{forward}}
11
12void test_f1(Incomplete *incomplete_p, int *int_p) {
13 f1(int_p);
14 f1(incomplete_p); // expected-note{{instantiation of exception spec}}
15}
16
17template<typename T> struct A {
18 template<typename U> struct B {
19 static void f() noexcept(A<U>().n);
20 };
21
22 constexpr A() : n(true) {}
23 bool n;
24};
25
26static_assert(noexcept(A<int>::B<char>::f()), "");
27
28template<unsigned N> struct S {
29 static void recurse() noexcept(noexcept(S<N+1>::recurse())); // \
30 // expected-error {{no member named 'recurse'}} \
31 // expected-note 9{{instantiation of exception spec}}
32};
33decltype(S<0>::recurse()) *pVoid1 = 0; // ok, exception spec not needed
34decltype(&S<0>::recurse) pFn = 0; // ok, exception spec not needed
35
36template<> struct S<10> {};
Richard Smithd3729422012-04-19 00:08:28 +000037void (*pFn2)() noexcept = &S<0>::recurse; // expected-note {{instantiation of exception spec}} expected-error {{not superset}}
Richard Smithf623c962012-04-17 00:58:00 +000038
39
Richard Smithf623c962012-04-17 00:58:00 +000040namespace dr1330_example {
41 template <class T> struct A {
42 void f(...) throw (typename T::X); // expected-error {{'int'}}
43 void f(int);
44 };
45
46 int main() {
47 A<int>().f(42);
48 }
49
David Majnemer766e2592013-10-22 04:14:18 +000050 struct S {
51 template<typename T>
52 static int f() noexcept(noexcept(A<T>().f("boo!"))) { return 0; } // \
53 // expected-note {{instantiation of exception spec}}
54 typedef decltype(f<S>()) X;
55 };
56
Richard Smithf623c962012-04-17 00:58:00 +000057 int test2() {
Richard Smithf623c962012-04-17 00:58:00 +000058 S().f<S>(); // ok
59 S().f<int>(); // expected-note {{instantiation of exception spec}}
60 }
61}
62
63namespace core_19754_example {
64 template<typename T> T declval() noexcept;
65
66 template<typename T, typename = decltype(T(declval<T&&>()))>
67 struct is_movable { static const bool value = true; };
68
69 template<typename T>
70 struct wrap {
71 T val;
72 void irrelevant(wrap &p) noexcept(is_movable<T>::value);
73 };
74
75 template<typename T>
76 struct base {
77 base() {}
78 base(const typename T::type1 &);
79 base(const typename T::type2 &);
80 };
81
82 template<typename T>
83 struct type1 {
84 wrap<typename T::base> base;
85 };
86
87 template<typename T>
88 struct type2 {
89 wrap<typename T::base> base;
90 };
91
92 struct types {
93 typedef base<types> base;
94 typedef type1<types> type1;
95 typedef type2<types> type2;
96 };
97
98 base<types> val = base<types>();
99}
Richard Smith79a52e52012-04-17 22:30:01 +0000100
101namespace pr9485 {
102 template <typename T> void f1(T) throw(typename T::exception); // expected-note {{candidate}}
103 template <typename T> void f1(T, int = 0) throw(typename T::noitpecxe); // expected-note {{candidate}}
104
105 template <typename T> void f2(T) noexcept(T::throws); // expected-note {{candidate}}
106 template <typename T> void f2(T, int = 0) noexcept(T::sworht); // expected-note {{candidate}}
107
108 void test() {
109 f1(0); // expected-error {{ambiguous}}
110 f2(0); // expected-error {{ambiguous}}
111 }
112}
Richard Smithd3729422012-04-19 00:08:28 +0000113
114struct Exc1 { char c[4]; };
115struct Exc2 { double x, y, z; };
116struct Base {
117 virtual void f() noexcept; // expected-note {{overridden}}
118};
119template<typename T> struct Derived : Base {
120 void f() noexcept (sizeof(T) == 4); // expected-error {{is more lax}}
121 void g() noexcept (T::error);
122};
123
124Derived<Exc1> d1; // ok
125Derived<Exc2> d2; // expected-note {{in instantiation of}}
Richard Smith4ff9ff92012-07-07 06:59:51 +0000126
127// If the vtable for a derived class is used, the exception specification of
128// any member function which ends up in that vtable is needed, even if it was
129// declared in a base class.
130namespace PR12763 {
131 template<bool *B> struct T {
132 virtual void f() noexcept (*B); // expected-error {{constant expression}} expected-note {{read of non-const}}
133 };
134 bool b; // expected-note {{here}}
135 struct X : public T<&b> {
136 virtual void g();
137 };
138 void X::g() {} // expected-note {{in instantiation of}}
139}