1//===----------------------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9// Can a noexcept function pointer be caught by a non-noexcept catch clause?
10// UNSUPPORTED: c++03, c++11, c++14
11// UNSUPPORTED: no-exceptions
12
13#include <cassert>
14
15template<bool Noexcept> void f() noexcept(Noexcept) {}
16template<bool Noexcept> using FnType = void() noexcept(Noexcept);
17
18template<bool ThrowNoexcept, bool CatchNoexcept>
19void check()
20{
21 try
22 {
23 auto *p = f<ThrowNoexcept>;
24 throw p;
25 assert(false);
26 }
27 catch (FnType<CatchNoexcept> *p)
28 {
29 assert(ThrowNoexcept || !CatchNoexcept);
30 assert(p == &f<ThrowNoexcept>);
31 }
32 catch (...)
33 {
34 assert(!ThrowNoexcept && CatchNoexcept);
35 }
36}
37
38void check_deep() {
39 auto *p = f<true>;
40 try
41 {
42 throw &p;
43 }
44 catch (FnType<false> **q)
45 {
46 assert(false);
47 }
48 catch (FnType<true> **q)
49 {
50 }
51 catch (...)
52 {
53 assert(false);
54 }
55}
56
57int main(int, char**)
58{
59 check<false, false>();
60 check<false, true>();
61 check<true, false>();
62 check<true, true>();
63 check_deep();
64
65 return 0;
66}
67

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of libcxxabi/test/catch_function_03.pass.cpp