1 //===--------------------- catch_pointer_nullptr.cpp ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <cassert> 11 #include <cstdlib> 12 13 #ifndef __has_feature 14 #define __has_feature(x) 0 15 #endif 16 17 struct A {}; 18 19 #if __has_feature(cxx_nullptr) 20 21 void test1() 22 { 23 try 24 { 25 throw nullptr; 26 assert(false); 27 } 28 catch (int*) 29 { 30 } 31 catch (long*) 32 { 33 assert(false); 34 } 35 } 36 37 void test2() 38 { 39 try 40 { 41 throw nullptr; 42 assert(false); 43 } 44 catch (A*) 45 { 46 } 47 catch (int*) 48 { 49 assert(false); 50 } 51 } 52 53 template <class Catch> 54 void catch_nullptr_test() { 55 try { 56 throw nullptr; 57 assert(false); 58 } catch (Catch) { 59 // nothing todo 60 } catch (...) { 61 assert(false); 62 } 63 } 64 65 #else 66 67 void test1() 68 { 69 } 70 71 void test2() 72 { 73 } 74 75 template <class Catch> 76 void catch_nullptr_test() 77 { 78 } 79 80 #endif 81 82 int main() 83 { 84 // catch naked nullptrs 85 test1(); 86 test2(); 87 88 catch_nullptr_test<int*>(); 89 catch_nullptr_test<int**>(); 90 catch_nullptr_test<int A::*>(); 91 catch_nullptr_test<const int A::*>(); 92 catch_nullptr_test<int A::**>(); 93 } 94