blob: 7de65835f73a04aca2036b449ab9ac103ab13d11 [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_callable
15
16// Most testing of is_callable is done within the [meta.trans.other] result_of
17// tests.
18
19#include <type_traits>
20#include <functional>
21
22#include "test_macros.h"
23
24struct Tag {};
25struct DerFromTag : Tag {};
26
27struct Implicit {
28 Implicit(int) {}
29};
30
31struct Explicit {
32 explicit Explicit(int) {}
33};
34
35struct NotCallableWithInt {
36 int operator()(int) = delete;
37 int operator()(Tag) { return 42; }
38};
39
40int main()
41{
42 {
43 using Fn = int(Tag::*)(int);
44 using RFn = int(Tag::*)(int) &&;
45 // INVOKE bullet 1, 2 and 3
46 {
47 // Bullet 1
48 static_assert(std::is_callable<Fn(Tag&, int)>::value);
49 static_assert(std::is_callable<Fn(DerFromTag&, int)>::value);
50 static_assert(std::is_callable<RFn(Tag&&, int)>::value);
51 static_assert(!std::is_callable<RFn(Tag&, int)>::value);
52 static_assert(!std::is_callable<Fn(Tag&)>::value);
53 static_assert(!std::is_callable<Fn(Tag const&, int)>::value);
54 }
55 {
56 // Bullet 2
57 using T = std::reference_wrapper<Tag>;
58 using DT = std::reference_wrapper<DerFromTag>;
59 using CT = std::reference_wrapper<const Tag>;
60 static_assert(std::is_callable<Fn(T&, int)>::value);
61 static_assert(std::is_callable<Fn(DT&, int)>::value);
62 static_assert(std::is_callable<Fn(const T&, int)>::value);
63 static_assert(std::is_callable<Fn(T&&, int)>::value);
64 static_assert(!std::is_callable<Fn(CT&, int)>::value);
65 static_assert(!std::is_callable<RFn(T, int)>::value);
66 }
67 {
68 // Bullet 3
69 using T = Tag*;
70 using DT = DerFromTag*;
71 using CT = const Tag*;
72 using ST = std::unique_ptr<Tag>;
73 static_assert(std::is_callable<Fn(T&, int)>::value);
74 static_assert(std::is_callable<Fn(DT&, int)>::value);
75 static_assert(std::is_callable<Fn(const T&, int)>::value);
76 static_assert(std::is_callable<Fn(T&&, int)>::value);
77 static_assert(std::is_callable<Fn(ST, int)>::value);
78 static_assert(!std::is_callable<Fn(CT&, int)>::value);
79 static_assert(!std::is_callable<RFn(T, int)>::value);
80 }
81 }
82 {
83 // Bullets 4, 5 and 6
84 using Fn = int (Tag::*);
85 static_assert(!std::is_callable<Fn()>::value);
86 {
87 // Bullet 4
88 static_assert(std::is_callable<Fn(Tag&)>::value);
89 static_assert(std::is_callable<Fn(DerFromTag&)>::value);
90 static_assert(std::is_callable<Fn(Tag&&)>::value);
91 static_assert(std::is_callable<Fn(Tag const&)>::value);
92 }
93 {
94 // Bullet 5
95 using T = std::reference_wrapper<Tag>;
96 using DT = std::reference_wrapper<DerFromTag>;
97 using CT = std::reference_wrapper<const Tag>;
98 static_assert(std::is_callable<Fn(T&)>::value);
99 static_assert(std::is_callable<Fn(DT&)>::value);
100 static_assert(std::is_callable<Fn(const T&)>::value);
101 static_assert(std::is_callable<Fn(T&&)>::value);
102 static_assert(std::is_callable<Fn(CT&)>::value);
103 }
104 {
105 // Bullet 6
106 using T = Tag*;
107 using DT = DerFromTag*;
108 using CT = const Tag*;
109 using ST = std::unique_ptr<Tag>;
110 static_assert(std::is_callable<Fn(T&)>::value);
111 static_assert(std::is_callable<Fn(DT&)>::value);
112 static_assert(std::is_callable<Fn(const T&)>::value);
113 static_assert(std::is_callable<Fn(T&&)>::value);
114 static_assert(std::is_callable<Fn(ST)>::value);
115 static_assert(std::is_callable<Fn(CT&)>::value);
116 }
117 }
118 {
119 // INVOKE bullet 7
120 {
121 // Function pointer
122 using Fp = void(*)(Tag&, int);
123 static_assert(std::is_callable<Fp(Tag&, int)>::value);
124 static_assert(std::is_callable<Fp(DerFromTag&, int)>::value);
125 static_assert(!std::is_callable<Fp(const Tag&, int)>::value);
126 static_assert(!std::is_callable<Fp()>::value);
127 static_assert(!std::is_callable<Fp(Tag&)>::value);
128 }
129 {
130 // Function reference
131 using Fp = void(&)(Tag&, int);
132 static_assert(std::is_callable<Fp(Tag&, int)>::value);
133 static_assert(std::is_callable<Fp(DerFromTag&, int)>::value);
134 static_assert(!std::is_callable<Fp(const Tag&, int)>::value);
135 static_assert(!std::is_callable<Fp()>::value);
136 static_assert(!std::is_callable<Fp(Tag&)>::value);
137 }
138 {
139 // Function object
140 using Fn = NotCallableWithInt;
141 static_assert(std::is_callable<Fn(Tag)>::value, "");
142 static_assert(!std::is_callable<Fn(int)>::value, "");
143 }
144 }
145 {
146 // Check that the conversion to the return type is properly checked
147 using Fn = int(*)();
148 static_assert(std::is_callable<Fn(), Implicit>::value);
149 static_assert(std::is_callable<Fn(), double>::value);
150 static_assert(std::is_callable<Fn(), const volatile void>::value);
151 static_assert(!std::is_callable<Fn(), Explicit>::value);
152 }
153 {
154 // Check for is_callable_v
155 using Fn = void(*)();
156 static_assert(std::is_callable_v<Fn()>);
157 static_assert(!std::is_callable_v<Fn(int)>);
158 }
159}