blob: 5cadc0bf8cbfc53b553ecb60fac16ed3b94f61b0 [file] [log] [blame]
Eric Fiselier8d5cbd72016-04-20 00:14:32 +00001//===----------------------------------------------------------------------===//
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// UNSUPPORTED: c++98, c++03, c++11, c++14
11
12// type_traits
13
14// is_nothrow_callable
15
16#include <type_traits>
17#include <functional>
18
19#include "test_macros.h"
20
21struct Tag {};
22
23struct Implicit {
24 Implicit(int) noexcept {}
25};
26
27struct ThrowsImplicit {
28 ThrowsImplicit(int) {}
29};
30
31struct Explicit {
32 explicit Explicit(int) noexcept {}
33};
34
35template <bool IsNoexcept, class Ret, class ...Args>
36struct CallObject {
37 Ret operator()(Args&&...) const noexcept(IsNoexcept);
38};
39
40template <class Fn>
41constexpr bool throws_callable() {
42 return std::is_callable<Fn>::value &&
43 !std::is_nothrow_callable<Fn>::value;
44}
45
46template <class Fn, class Ret>
47constexpr bool throws_callable() {
48 return std::is_callable<Fn, Ret>::value &&
49 !std::is_nothrow_callable<Fn, Ret>::value;
50}
51
52// FIXME(EricWF) Don't test the where noexcept is *not* part of the type system
53// once implementations have caught up.
54void test_noexcept_function_pointers()
55{
Eric Fiselierce24f962016-04-28 02:18:48 +000056 struct Dummy { void foo() noexcept {} static void bar() noexcept {} };
Eric Fiselier8d5cbd72016-04-20 00:14:32 +000057#if !defined(__cpp_noexcept_function_type)
58 {
59 // Check that PMF's and function pointers *work*. is_nothrow_callable will always
60 // return false because 'noexcept' is not part of the function type.
61 static_assert(throws_callable<decltype(&Dummy::foo)(Dummy&)>());
62 static_assert(throws_callable<decltype(&Dummy::bar)()>());
63 }
64#else
65 {
66 // Check that PMF's and function pointers actually work and that
67 // is_nothrow_callable returns true for noexcept PMF's and function
68 // pointers.
69 static_assert(std::is_nothrow_callable<decltype(&Dummy::foo)(Dummy&)>::value);
70 static_assert(std::is_nothrow_callable<decltype(&Dummy::bar)()>::value);
71 }
72#endif
73}
74
75int main()
76{
77 {
78 // Check that the conversion to the return type is properly checked
79 using Fn = CallObject<true, int>;
80 static_assert(std::is_nothrow_callable<Fn(), Implicit>::value);
81 static_assert(std::is_nothrow_callable<Fn(), double>::value);
82 static_assert(std::is_nothrow_callable<Fn(), const volatile void>::value);
83 static_assert(throws_callable<Fn(), ThrowsImplicit>());
84 static_assert(!std::is_nothrow_callable<Fn(), Explicit>());
85 }
86 {
87 // Check that the conversion to the parameters is properly checked
88 using Fn = CallObject<true, void, const Implicit&, const ThrowsImplicit&>;
89 static_assert(std::is_nothrow_callable<Fn(Implicit&, ThrowsImplicit&)>::value);
90 static_assert(std::is_nothrow_callable<Fn(int, ThrowsImplicit&)>::value);
91 static_assert(throws_callable<Fn(int, int)>());
92 static_assert(!std::is_nothrow_callable<Fn()>::value);
93 }
94 {
95 // Check that the noexcept-ness of function objects is checked.
96 using Fn = CallObject<true, void>;
97 using Fn2 = CallObject<false, void>;
98 static_assert(std::is_nothrow_callable<Fn()>::value);
99 static_assert(throws_callable<Fn2()>());
100 }
101 {
102 // Check that PMD derefs are noexcept
103 using Fn = int (Tag::*);
104 static_assert(std::is_nothrow_callable<Fn(Tag&)>::value);
105 static_assert(std::is_nothrow_callable<Fn(Tag&), Implicit>::value);
106 static_assert(throws_callable<Fn(Tag&), ThrowsImplicit>());
107 }
108 {
109 // Check for is_nothrow_callable_v
110 using Fn = CallObject<true, int>;
111 static_assert(std::is_nothrow_callable_v<Fn()>);
112 static_assert(!std::is_nothrow_callable_v<Fn(int)>);
113 }
114 test_noexcept_function_pointers();
115}