Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 1 | //===----------------------- catch_function_01.cpp ------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // Can you have a catch clause of array type that catches anything? |
| 10 | |
Eric Fiselier | 3f7c207 | 2016-01-20 04:06:46 +0000 | [diff] [blame] | 11 | // GCC incorrectly allows function pointer to be caught by reference. |
| 12 | // See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69372 |
| 13 | // XFAIL: gcc |
Louis Dionne | 8c61114 | 2020-04-17 10:29:15 -0400 | [diff] [blame] | 14 | // UNSUPPORTED: no-exceptions |
Eric Fiselier | 3f7c207 | 2016-01-20 04:06:46 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 16 | #include <cassert> |
| 17 | |
Eric Fiselier | 65ace9d | 2015-05-01 01:49:37 +0000 | [diff] [blame] | 18 | template <class Tp> |
| 19 | bool can_convert(Tp) { return true; } |
| 20 | |
| 21 | template <class> |
| 22 | bool can_convert(...) { return false; } |
| 23 | |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 24 | void f() {} |
| 25 | |
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 26 | int main(int, char**) |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 27 | { |
| 28 | typedef void Function(); |
Eric Fiselier | 65ace9d | 2015-05-01 01:49:37 +0000 | [diff] [blame] | 29 | assert(!can_convert<Function&>(&f)); |
| 30 | assert(!can_convert<void*>(&f)); |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 31 | try |
| 32 | { |
| 33 | throw f; // converts to void (*)() |
| 34 | assert(false); |
| 35 | } |
| 36 | catch (Function& b) // can't catch void (*)() |
| 37 | { |
| 38 | assert(false); |
| 39 | } |
Eric Fiselier | 65ace9d | 2015-05-01 01:49:37 +0000 | [diff] [blame] | 40 | catch (void*) // can't catch as void* |
| 41 | { |
| 42 | assert(false); |
| 43 | } |
| 44 | catch(Function*) |
| 45 | { |
| 46 | } |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 47 | catch (...) |
| 48 | { |
Eric Fiselier | 65ace9d | 2015-05-01 01:49:37 +0000 | [diff] [blame] | 49 | assert(false); |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 50 | } |
Louis Dionne | 504bc07 | 2020-10-08 13:36:33 -0400 | [diff] [blame] | 51 | |
| 52 | return 0; |
Howard Hinnant | a21f8c2 | 2012-02-01 19:42:45 +0000 | [diff] [blame] | 53 | } |