1 //===----------------------------------------------------------------------===//
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 // <functional>
11 
12 // template<class F>
13 // function(F) -> function<see-below>;
14 
15 // UNSUPPORTED: c++03, c++11, c++14
16 
17 #include <functional>
18 #include <type_traits>
19 #include <utility>
20 
21 #include "test_macros.h"
22 
23 struct R { };
24 struct A1 { };
25 struct A2 { };
26 struct A3 { };
27 
28 #define DECLARE_FUNCTIONS_WITH_QUALS(N, ...)                              \
29   struct f0_##N  { R operator()() __VA_ARGS__           { return {}; } }; \
30   struct f1_##N  { R operator()(A1) __VA_ARGS__         { return {}; } }; \
31   struct f2_##N  { R operator()(A1, A2) __VA_ARGS__     { return {}; } }; \
32   struct f3_##N  { R operator()(A1, A2, A3) __VA_ARGS__ { return {}; } }  \
33 /**/
34 
35 DECLARE_FUNCTIONS_WITH_QUALS(0, /* nothing */);
36 DECLARE_FUNCTIONS_WITH_QUALS(1, const);
37 DECLARE_FUNCTIONS_WITH_QUALS(2, volatile);
38 DECLARE_FUNCTIONS_WITH_QUALS(3, const volatile);
39 DECLARE_FUNCTIONS_WITH_QUALS(4, &);
40 DECLARE_FUNCTIONS_WITH_QUALS(5 , const &);
41 DECLARE_FUNCTIONS_WITH_QUALS(6 , volatile &);
42 DECLARE_FUNCTIONS_WITH_QUALS(7 , const volatile &);
43 DECLARE_FUNCTIONS_WITH_QUALS(8 , noexcept);
44 DECLARE_FUNCTIONS_WITH_QUALS(9 , const noexcept);
45 DECLARE_FUNCTIONS_WITH_QUALS(10, volatile noexcept);
46 DECLARE_FUNCTIONS_WITH_QUALS(11, const volatile noexcept);
47 DECLARE_FUNCTIONS_WITH_QUALS(12, & noexcept);
48 DECLARE_FUNCTIONS_WITH_QUALS(13, const & noexcept);
49 DECLARE_FUNCTIONS_WITH_QUALS(14, volatile & noexcept);
50 DECLARE_FUNCTIONS_WITH_QUALS(15, const volatile & noexcept);
51 
main(int,char **)52 int main(int, char**) {
53 #define CHECK_FUNCTIONS(N)                                                    \
54   do {                                                                        \
55     /* implicit */                                                            \
56     std::function g0 = f0_##N{};                                              \
57     ASSERT_SAME_TYPE(decltype(g0), std::function<R()>);                       \
58                                                                               \
59     std::function g1 = f1_##N{};                                              \
60     ASSERT_SAME_TYPE(decltype(g1), std::function<R(A1)>);                     \
61                                                                               \
62     std::function g2 = f2_##N{};                                              \
63     ASSERT_SAME_TYPE(decltype(g2), std::function<R(A1, A2)>);                 \
64                                                                               \
65     std::function g3 = f3_##N{};                                              \
66     ASSERT_SAME_TYPE(decltype(g3), std::function<R(A1, A2, A3)>);             \
67                                                                               \
68     /* explicit */                                                            \
69     std::function g4{f0_##N{}};                                               \
70     ASSERT_SAME_TYPE(decltype(g4), std::function<R()>);                       \
71                                                                               \
72     std::function g5{f1_##N{}};                                               \
73     ASSERT_SAME_TYPE(decltype(g5), std::function<R(A1)>);                     \
74                                                                               \
75     std::function g6{f2_##N{}};                                               \
76     ASSERT_SAME_TYPE(decltype(g6), std::function<R(A1, A2)>);                 \
77                                                                               \
78     std::function g7{f3_##N{}};                                               \
79     ASSERT_SAME_TYPE(decltype(g7), std::function<R(A1, A2, A3)>);             \
80                                                                               \
81     /* from std::function */                                                  \
82     std::function<R(A1)> unary;                                               \
83     std::function g8 = unary;                                                 \
84     ASSERT_SAME_TYPE(decltype(g8), std::function<R(A1)>);                     \
85                                                                               \
86     std::function g9 = std::move(unary);                                      \
87     ASSERT_SAME_TYPE(decltype(g9), std::function<R(A1)>);                     \
88                                                                               \
89     std::function<R(A1&&)> unary_ref;                                         \
90     std::function g10 = unary_ref;                                            \
91     ASSERT_SAME_TYPE(decltype(g10), std::function<R(A1&&)>);                  \
92                                                                               \
93     std::function g11 = std::move(unary_ref);                                 \
94     ASSERT_SAME_TYPE(decltype(g11), std::function<R(A1&&)>);                  \
95   } while (false)                                                             \
96 /**/
97 
98   // Make sure we can deduce from function objects with valid call operators
99   CHECK_FUNCTIONS(0);
100   CHECK_FUNCTIONS(1);
101   CHECK_FUNCTIONS(2);
102   CHECK_FUNCTIONS(3);
103   CHECK_FUNCTIONS(4);
104   CHECK_FUNCTIONS(5);
105   CHECK_FUNCTIONS(6);
106   CHECK_FUNCTIONS(7);
107   CHECK_FUNCTIONS(8);
108   CHECK_FUNCTIONS(9);
109   CHECK_FUNCTIONS(10);
110   CHECK_FUNCTIONS(11);
111   CHECK_FUNCTIONS(12);
112   CHECK_FUNCTIONS(13);
113   CHECK_FUNCTIONS(14);
114   CHECK_FUNCTIONS(15);
115 
116   return 0;
117 }
118 
119 // Make sure we fail in a SFINAE-friendly manner when we try to deduce
120 // from a type without a valid call operator.
121 template <typename F, typename = decltype(std::function{std::declval<F>()})>
can_deduce()122 constexpr bool can_deduce() { return true; }
123 template <typename F>
can_deduce(...)124 constexpr bool can_deduce(...) { return false; }
125 
126 struct invalid1 { };
127 struct invalid2 {
128   template <typename ...Args>
129   void operator()(Args ...);
130 };
131 struct invalid3 {
132   void operator()(int);
133   void operator()(long);
134 };
135 static_assert(!can_deduce<invalid1>());
136 static_assert(!can_deduce<invalid2>());
137 static_assert(!can_deduce<invalid3>());
138