• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class functor1
20     : public std::unary_function<int, char>
21 {
22 };
23 
24 class functor2
25     : public std::binary_function<char, int, double>
26 {
27 };
28 
29 class functor3
30     : public std::unary_function<char, int>,
31       public std::binary_function<char, int, double>
32 {
33 public:
34     typedef float result_type;
35 };
36 
37 class functor4
38     : public std::unary_function<char, int>,
39       public std::binary_function<char, int, double>
40 {
41 public:
42 };
43 
44 class C {};
45 
46 template <class T>
47 struct has_result_type
48 {
49 private:
50     struct two {char _; char __;};
51     template <class U> static two test(...);
52     template <class U> static char test(typename U::result_type* = 0);
53 public:
54     static const bool value = sizeof(test<T>(0)) == 1;
55 };
56 
main()57 int main()
58 {
59     static_assert((std::is_same<std::reference_wrapper<functor1>::result_type,
60                                 char>::value), "");
61     static_assert((std::is_same<std::reference_wrapper<functor2>::result_type,
62                                 double>::value), "");
63     static_assert((std::is_same<std::reference_wrapper<functor3>::result_type,
64                                 float>::value), "");
65     static_assert((std::is_same<std::reference_wrapper<void()>::result_type,
66                                 void>::value), "");
67     static_assert((std::is_same<std::reference_wrapper<int*(double*)>::result_type,
68                                 int*>::value), "");
69     static_assert((std::is_same<std::reference_wrapper<void(*)()>::result_type,
70                                 void>::value), "");
71     static_assert((std::is_same<std::reference_wrapper<int*(*)(double*)>::result_type,
72                                 int*>::value), "");
73     static_assert((std::is_same<std::reference_wrapper<int*(C::*)(double*)>::result_type,
74                                 int*>::value), "");
75     static_assert((std::is_same<std::reference_wrapper<int (C::*)(double*) const volatile>::result_type,
76                                 int>::value), "");
77     static_assert((std::is_same<std::reference_wrapper<C()>::result_type,
78                                 C>::value), "");
79     static_assert(has_result_type<std::reference_wrapper<functor3> >::value, "");
80     static_assert(!has_result_type<std::reference_wrapper<functor4> >::value, "");
81     static_assert(!has_result_type<std::reference_wrapper<C> >::value, "");
82 }
83