1 // Test for noexcept-expression 2 // { dg-do compile { target c++11 } } 3 // { dg-options "-O2" } 4 5 #include <typeinfo> 6 7 #define SA(X) static_assert(X, #X) 8 9 void f(); 10 void g() throw(); 11 SA(noexcept(g())); 12 SA(!noexcept(f())); 13 SA(!noexcept(throw 1)); 14 SA(noexcept(42)); 15 16 struct A 17 { 18 virtual ~A(); 19 }; 20 21 struct B: public A 22 { 23 virtual ~B(); 24 }; 25 26 A* ap; 27 28 struct C { }; 29 C* cp; 30 31 SA (noexcept (dynamic_cast<B*>(ap))); 32 SA (!noexcept (dynamic_cast<B&>(*ap))); 33 SA (!noexcept (typeid (*ap))); 34 SA (noexcept (typeid (*cp))); 35 36 SA (!noexcept (true ? 1 : throw 1)); 37 SA (!noexcept (true || true ? 1 : throw 1)); 38 39 SA (noexcept (C())); 40 41 struct D 42 { 43 D() throw(); 44 }; 45 46 SA (noexcept (D())); 47 48 struct E 49 { 50 E() throw(); 51 ~E(); 52 }; 53 54 SA (noexcept (E())); 55 56 struct F 57 { 58 virtual void f(); 59 }; 60 61 SA (noexcept (F())); 62 63 struct G 64 { 65 G() = default; 66 ~G() = default; 67 }; 68 69 SA (noexcept (G())); 70 71 template <class T, bool b> tf()72void tf() 73 { 74 SA (noexcept (T()) == b); 75 } 76 77 template void tf<int,true>(); 78 template void tf<E, true>(); 79 80 // Make sure that noexcept uses the declared exception-specification, not 81 // any knowledge we might have about whether or not the function really 82 // throws. h()83void h() { } 84 SA(!noexcept(h())); 85