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