OSDN Git Service

d9922454425b41a4f8db4ef8a96b7d5412a45a51
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.dg / cpp0x / noexcept03.C
1 // Runtime test for noexcept-specification.
2 // { dg-options "-std=c++0x" }
3 // { dg-do run }
4
5 #include <exception>
6 #include <cstdlib>
7
8 void my_terminate ()
9 {
10   std::exit (0);
11 }
12
13 void my_unexpected ()
14 {
15   throw;
16 }
17
18 void g() { throw 1; }
19 void (*p)() = g;
20 void f () noexcept (false)
21 {
22   p();
23 }
24
25 template <class T>
26 void f(T) noexcept (noexcept (T()))
27 {
28   p();
29 }
30
31 template <class T>
32 void f2(T a) noexcept (noexcept (f (a)))
33 {
34   f(a);
35 }
36
37 struct A { A() { } };
38
39 // throw(int) overrides noexcept(false) in either order.
40 void h() throw (int, std::bad_exception);
41 void h() noexcept (false)
42 {
43   throw 1.0;
44 }
45
46 void i() noexcept (false);
47 void i() throw (int, std::bad_exception)
48 {
49   throw 1.0;
50 }
51
52 int main()
53 {
54   // noexcept(false) allows throw.
55   try { f(); } catch (int) { }
56   // noexcept(noexcept(A())) == noexcept(false).
57   try { f(A()); } catch (int) { }
58   try { f2(A()); } catch (int) { }
59
60   std::set_unexpected (my_unexpected);
61   try { h(); } catch (std::bad_exception) { }
62   try { i(); } catch (std::bad_exception) { }
63
64   std::set_terminate (my_terminate);
65   // noexcept(noexcept(int())) == noexcept(true).
66   try { f2(1); } catch (...) { }
67   return 1;
68 }