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