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 // reference_wrapper
13 
14 // has weak result type
15 
16 #include <functional>
17 #include <type_traits>
18 
19 template <class Arg, class Result>
20 struct my_unary_function
21 { // std::unary_function was removed in C++17
22     typedef Arg argument_type;
23     typedef Result result_type;
24 };
25 
26 template <class Arg1, class Arg2, class Result>
27 struct my_binary_function
28 { // std::binary_function was removed in C++17
29     typedef Arg1 first_argument_type;
30     typedef Arg2 second_argument_type;
31     typedef Result result_type;
32 };
33 
34 class functor1
35     : public my_unary_function<int, char>
36 {
37 };
38 
39 class functor2
40     : public my_binary_function<char, int, double>
41 {
42 };
43 
44 class functor3
45     : public my_unary_function<char, int>,
46       public my_binary_function<char, int, double>
47 {
48 public:
49     typedef float result_type;
50 };
51 
52 class functor4
53     : public my_unary_function<char, int>,
54       public my_binary_function<char, int, double>
55 {
56 public:
57 };
58 
59 class C {};
60 
61 template <class T>
62 struct has_result_type
63 {
64 private:
65     struct two {char _; char __;};
66     template <class U> static two test(...);
67     template <class U> static char test(typename U::result_type* = 0);
68 public:
69     static const bool value = sizeof(test<T>(0)) == 1;
70 };
71 
main()72 int main()
73 {
74     static_assert((std::is_same<std::reference_wrapper<functor1>::result_type,
75                                 char>::value), "");
76     static_assert((std::is_same<std::reference_wrapper<functor2>::result_type,
77                                 double>::value), "");
78     static_assert((std::is_same<std::reference_wrapper<functor3>::result_type,
79                                 float>::value), "");
80     static_assert((std::is_same<std::reference_wrapper<void()>::result_type,
81                                 void>::value), "");
82     static_assert((std::is_same<std::reference_wrapper<int*(double*)>::result_type,
83                                 int*>::value), "");
84     static_assert((std::is_same<std::reference_wrapper<void(*)()>::result_type,
85                                 void>::value), "");
86     static_assert((std::is_same<std::reference_wrapper<int*(*)(double*)>::result_type,
87                                 int*>::value), "");
88     static_assert((std::is_same<std::reference_wrapper<int*(C::*)(double*)>::result_type,
89                                 int*>::value), "");
90     static_assert((std::is_same<std::reference_wrapper<int (C::*)(double*) const volatile>::result_type,
91                                 int>::value), "");
92     static_assert((std::is_same<std::reference_wrapper<C()>::result_type,
93                                 C>::value), "");
94     static_assert(has_result_type<std::reference_wrapper<functor3> >::value, "");
95     static_assert(!has_result_type<std::reference_wrapper<functor4> >::value, "");
96     static_assert(!has_result_type<std::reference_wrapper<C> >::value, "");
97 }
98