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