| 1 |
// Runtime test for noexcept-specification. |
| 2 |
// { dg-options "-Wnoexcept" } |
| 3 |
// { dg-do run { target { nonpic || pie_enabled } } } |
| 4 |
// { dg-require-effective-target c++11 } |
| 5 |
|
| 6 |
#include <exception> |
| 7 |
#include <cstdlib> |
| 8 |
|
| 9 |
void my_terminate () |
| 10 |
{ |
| 11 |
std::exit (0); |
| 12 |
} |
| 13 |
|
| 14 |
void my_unexpected () |
| 15 |
{ |
| 16 |
throw; |
| 17 |
} |
| 18 |
|
| 19 |
void g() { throw 1; } |
| 20 |
void (*p)() = g; |
| 21 |
void f () noexcept (false) |
| 22 |
{ |
| 23 |
p(); |
| 24 |
} |
| 25 |
|
| 26 |
template <class T> |
| 27 |
void f(T) noexcept (noexcept (T())) // { dg-warning "false" } |
| 28 |
{ |
| 29 |
p(); |
| 30 |
} |
| 31 |
|
| 32 |
template <class T> |
| 33 |
void f2(T a) noexcept (noexcept (f (a))) |
| 34 |
{ |
| 35 |
f(a); |
| 36 |
} |
| 37 |
|
| 38 |
struct A { A() { } }; // { dg-message "does not throw" } |
| 39 |
|
| 40 |
int main() |
| 41 |
{ |
| 42 |
// noexcept(false) allows throw. |
| 43 |
try { f(); } catch (int) { } |
| 44 |
// noexcept(noexcept(A())) == noexcept(false). |
| 45 |
try { f(A()); } catch (int) { } |
| 46 |
try { f2(A()); } catch (int) { } |
| 47 |
|
| 48 |
std::set_terminate (my_terminate); |
| 49 |
// noexcept(noexcept(int())) == noexcept(true). |
| 50 |
try { f2(1); } catch (...) { } |
| 51 |
return 1; |
| 52 |
} |