xref: /llvm-project/libcxx/test/std/utilities/function.objects/refwrap/refwrap.helpers/ref_2.pass.cpp (revision a13c10588cd57bcef93f2a27a74dd7c32bc6850b)
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 // template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T> t);
14 
15 #include <functional>
16 #include <cassert>
17 
18 #include "counting_predicates.h"
19 
20 #include "test_macros.h"
21 
is5(int i)22 bool is5 ( int i ) { return i == 5; }
23 
24 template <typename T>
call_pred(T pred)25 bool call_pred ( T pred ) { return pred(5); }
26 
27 namespace adl {
28   struct A {};
ref(A)29   void ref(A) {}
30 }
31 
test()32 TEST_CONSTEXPR_CXX20 bool test()
33 {
34   {
35     int i = 0;
36     std::reference_wrapper<int> r1 = std::ref(i);
37     std::reference_wrapper<int> r2 = std::ref(r1);
38     assert(&r2.get() == &i);
39   }
40   {
41     adl::A a;
42     std::reference_wrapper<adl::A> a1 = std::ref(a);
43     std::reference_wrapper<adl::A> a2 = std::ref(a1);
44     assert(&a2.get() == &a);
45   }
46   return true;
47 }
48 
main(int,char **)49 int main(int, char**)
50 {
51   test();
52 #if TEST_STD_VER > 17
53   static_assert(test());
54 #endif
55 
56   {
57     unary_counting_predicate<bool(*)(int), int> cp(is5);
58     assert(!cp(6));
59     assert(cp.count() == 1);
60     assert(call_pred(cp));
61     assert(cp.count() == 1);
62     assert(call_pred(std::ref(cp)));
63     assert(cp.count() == 2);
64   }
65 
66   return 0;
67 }
68